Retrieve functions from a global variable - php

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.

Related

How to access a method as a member variable

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.

Static variable in php without objects

I want to define a php variable that can be accessed in all the scripts and is initialized only one time per runtime. This is, like a static variable but with no object. I thought of using $GLOBALS but noticed the variables do not stick between script executions and I would have to instantiate the variable everytime. So how exactly can I accomplish this? Would I need to create an abstract class only to static the variable?
how about using $_SESSION? see http://www.w3schools.com/php/php_sessions.asp

what is $this->vars($vars) in codeigniter?

Here is an example.
$vars['jscss_src'] = "\n".implode("\n",$aSrc)."\n";
$this->vars($vars);
There is an expression $this->load->vars() but when googling $this->vars()
Nothing could be found. What is the meaning of it?
Thanks in advance :)
+) actually this expression is coded in the extended Loader core class in codeigniter. Could it be a reason?
This code almost certain is part of a member function of some class. The this is referencing the actual object, the method has been called on. vars is another member function of the same class.
According to the documentation:
$this->load->vars($array)
This function takes an associative array as input and generates
variables using the PHP extract function. This function produces the
same result as using the second parameter of the $this->load->view()
function above. The reason you might want to use this function
independently is if you would like to set some global variables in the
constructor of your controller and have them become available in any
view file loaded from any function. You can have multiple calls to
this function. The data get cached and merged into one array for
conversion to variables.
The load->vars function adds the array to an Loader class variable that is used in the load->view function where the array gets extracted. (Is used to pass variables to views)

PHP: Calling class functions from within a class

Starting a new PHP project and deciding that after a few years of PHP development, I'm thinking I should really start using PHP classes. I'm used to classes in the C++ world, so there's a few things I'm not quite sure about when porting this knowledge over to PHP.
In C++, you can automatically access any class variables without a prefix, in PHP, it appears that you need to prefix all such accesses (variables and function) with this->. I know what this is (or at least I think so, a pointer to the current class instance), but I'm not sure whether its required or preferred, or if there is any alternatives. My class will be using other functions within the same class (ie, itself) fairly heavily, so having to type this-> every time is going to get time consuming quite quickly.
The classes themselves are likely to be singletons, so I'm not sure whether to actually use a class, or just prefix the functions with an identifier.
It is required that you reference the object to which the member belongs in order to access the member.
Every method call or property access is prefixed with $variable-> - $this is a magic variable that refers to the current object instance. Its use is not optional.
This is (amongst other reasons) because not every function in PHP is a method, there are also global functions. If you reference a function without associating it with an object, it is assumed to be a global function.
As a side note, you should avoid the use of singletons in PHP - there is no performance/memory gain to be found from using them because each concurrently executing script is garden-walled into its own memory space.
The "pointer" (->) != C++ pointer.
$this means the current class and instance. All variables are accessed by using $this->variable; or $this->function();.
Static variables, and functions can be accessed using self::$variable or self::function()
Outside the class instance, you must indicate the class instance: $foo->variable; or $foo->function();
As far as I know, there is no way to access public/private/static/constant variables inside the class without using $this-> or self::
In reference to using an object of functions... up to you. Are you planning on expanding the code later to add more functions? Are all the functions somewhat related? If they are singleton functions, there is no harm in just writing a function instead of a class. It really just depends on what you are trying to accomplish.
Yes, you do have to use $this when accessing class method's or variables.
You need to remember that PHP doesn't require that variables be declared, so image the following situation:
<?
class A {
public $a = 'Hello World!';
function hello() {
$a = 'hello';
echo $a;
}
}
$object = new A();
echo $object->hello();
$a would be local scope and $this->a would be the class variable.

Get the observability of class methods in PHP

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.

Categories