how to use constant from class as an argument definition in php function?

前端 未结 2 441
遇见更好的自我
遇见更好的自我 2021-02-06 03:26

I have a class:

class FetchMode 
{
 const FetchAll = 0;
 const FetchOne = 1;
 const FetchRow = 2;}

and a function:

function get         


        
2条回答
  •  遥遥无期
    2021-02-06 04:18

    I don't know what you mean by

    would like to offer a list of possible choices in calling a function. Is it possible in php?

    but for the error part: Imagine you have a var, e.g. $foo. When you do echo $foo you don't get the name of the var but its value. This is because a var has a name and points to a value. Every access to the var basically returns the value it points to. Its the same with constants; You put the constant name in there, but basically you are pointing to your stored value. Which means getRecordSet(FetchMode::FetchOne); and getRecordSet(1); is the same.

    So, getRecordSet(FetchMode $FetchMode) raises must be an instance of FetchMode because FetchMode::FetchOne points to an integer.

    To fix this, you need to use getRecordSet(int $FetchMode).

提交回复
热议问题