I have a class, Place, with a function named thing(). I'd like to be able to access the method as a variable, something like $dc=new Place; echo $dc->thing;. How can I do that without creating a member variable?
Found what I was looking for!
I would need to overload PHP's __get() function.
Related
I'm having some trouble trying to write a plugin for CraftCMS (a CMS based on YII Framework). I'm trying to create a simple fieldtype that I'm going to use inside a matrix block.
So far the public funciton getInputHtml of my fieldtype class, contains only a
var_dump($this->element)
And it shows me, as expected:
object(Craft\MatrixBlockModel)#1121 (25) {
["elementType":protected]=>
string(11) "MatrixBlock" [.......]
The problem is that, as I try to change the previous
var_dump($this->element)
with a
var_dump($this->element->getOwner())
(or any other method/property of the MatrixBlockModel class), I obtain:
"Call to a member function getOwner() on a non-object"
The only think I thought is that it could have something to do with the magic method __call() overwritten in a class from which MatrixBlockModel inherit (actually, a parent of a parent of a parent...). But, trying to have a look to its code, it do not seem so.
It likely is something with __call as you suggest. What happens if you do:
$var = $this->element;
var_dump($var);
var_dump($var->getOwner());
I have a hunch that if you assign it to a temporary variable that might solve the issue.
just like the __call magic method we can use in PHP to hook a call to a non defined method, is there a way to hook a call to an undefined constant or variable?
Like A::B, where B doesn't exist.
No. Constants are evaluated at opcode compile time not runtime, so there's no way to 'catch' them. There is still an issue with php related to this where a parent class cannot call a childs constant inside a parents method.
You're probably looking for __get and __set. I'm not really sure how it works with static constants though, you might only be able to work with instanced objects. See G-Nugget's comment regarding using them statically.
Is it possible to retrieve all the functions included in a global variable?
I'm using a very complex plugin (not written by me) that call the functions in this way:
$GLOBALS['myplugin']->member->isActive()
Is there a way to retreive all the functions of $GLOBALS['myplugin']->member?
You may find some success with get_class_methods(), passing in the name of the class that member is an instance of, or the object instance itself.
The function get_classs_methods returns all methods of a class.
But, is there a function or technique to get the observability of these methods?
I want to list only the public methods.
The return value from get_class_methods depends on the scope you're calling it from; if you're calling it from outside the class, you'll only get the methods that are visible from the current scope. Calling it from a method inside the class will give you all the available methods from the class.
If you want more information or to query the class in a more detailed manner, you probably want to look at using reflection and the getMethods method.
This was incredibly surprising to see that PHP has no obvious function to do what I'm looking for, so I'll ask here.
How would you go about getting the number of arguments a particular function has outside of the function (just like func_num_args only from outside the function).
The solution can't actually execute the function (that would be defeating the purpose), and I'd like to do it (preferably) without any sort of reflection class.
Possible?
Oh, you can use the ReflectionFunction class, which inherits from ReflectionFunctionAbstract, which defines the getNumberOfParameters function:
$func_reflection = new ReflectionFunction('function_name');
$num_of_params = $func_reflection->getNumberOfParameters();
Note: This will only work on user functions, not class or instance functions.