Reputation:
I am seeking a way to know if the parameters passed to a method is a constant defined in a specific class. For example:
class MyClass {
const CONSTANT = 4;
const ANOTHER_CONSTANT = 5;
public function aMethod ($a_CONSTANT) {
// function code
}
}
$myClass = new MyClass();
$myClass->aMethod(MyClass::CONSTANT); // Fine
$myClass->aMethod(MyClass::ANOTHER_CONSTANT) ; // Still okay
$myClass->aMethod(4); // Not okay
$myClass->aMethod(OtherClass::VALUE); // No way
In the above code, first two calls to aMethod
are acceptable but I want the parser to give an error while the third and fourth call to aMethod
happens, since the value passed to aMethod
is not a constant of a particular class (MyClass
, in this case). Is there a syntax to typehint the parameter in aMethod
to achieve what I want?
Upvotes: 1
Views: 194
Reputation: 522016
When you pass "a constant", you're not passing the constant itself, you're passing its value. MyClass::CONSTANT
and 4
are exactly synonymous. There is no difference between them you could detect. In fact, the compiler replaces all mentions of "MyClass::CONSTANT
" with 4
at compile time. That's what constants are.
Upvotes: 1