Get the observability of class methods in PHP - 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.

Related

Can PHP class contain method not listed by function `get_class_methods` ? If so, how? If not, why does it appear to?

I am digging into some Magento code.
I'm looking at class Mage_Catalog_Model_Session
There is code in class Mage_Catalog_Block_Product_List_Toolbar extends Mage_Core_Block_Template function getCurrentOrder like this:
Mage::getSingleton('catalog/session')->getSortOrder();
Now, Mage::getSingleton('catalog/session') returns an object of type Mage_Catalog_Model_Session
So I would think that getSortOrder() is a method of that class, although the method seems like it would go with this toolbar class and not that session class.
This line should give me the list of methods of the class:
print_r(get_class_methods(get_class($_test)));
And it does seem to, but getSortOrder() is not listed.
So, which class is getSortOrder() a member of and how is it that it either A.) appears to be a member of a class it's not a member of, or B.) is a member of that class but does not appear in the get_class_methods() result set?
Sounds like getSortOrder() is an overloaded method. See here for more info on overloading. Taken from the link:
Overloading in PHP provides means to dynamically "create" properties and methods.
get_class_methods() wouldn't display overloaded methods because they are created on the spot when called (And therefore there's no way to know about them.) A lot of frameworks use overloading for getters.
Btw, see this answer for an example of how overloading a getter would be achieved.

To store as a class parameter or normal php variable

I am new to OO concepts in PHP. I have a class called MY_ controller which is my base class. All classes extend this class. I am using MVC architecture. I am using caching in my system now. So i load the cache variable in the constructor of my base class. I use normal php variable like $cacheVariable in my function to store the value from cache. I was wondering if it would serve any help if i store it as a class parameter and use like $this->cacheVariable? In each function i get cache value like $this->cache->get('cacheVariable'); will it help if i get value from $this->cacheVariable
If you want to be able to use $cacheVariable anywhere else besides your constructor you'll want to use $this
public function __construct() {
//...
$this->cacheVariable = $this->cache->get('cacheVariable');
//...
}
Also remember, if you want your children classes to inherit this variable from your base class you will need to set it as either public or protected. If it's private children will not inherit it.
When you say "class variable" I assume you are referring to a property (property of an instance=. Note the difference:
if you have a class, say MyClassA, then in your scripts you will instantiate that class using the operator new (depending on your PHP version you can use a different constructor syntax it changed since PHP-5.3.0):
<?php
class MY_controller{
public $cacheVariable;
// constructor
function MY_controller($aValue){
// constructor code which loads cacheVariable, for example with parameter
$this->cacheVariable = $aValue;
}
public function someFunction(){
//... some code, then access the property
$cv = $this->cacheVariable;
}
}
$aController = new MY_controller(42);
?>
As you inherit from MY_controller, every instance of that class, will have access to cacheVariable through $this->cacheVariable.
The important thing I wanted to clarify is that it is an instance property, not a class one. For further reference on OOP in PHP, refer to the properties section in PHP's OOP manual and the inheritance section.
When you instantiate only one instances of your controller (derived from your main controller), its a conceivable solution.
In my opinion the better idea is getting variable directly from cache service everywhere you want in your class and don't keep it as a class property. The reason is simple: let's say value of some key from cache will be changed (or expires) in the other place than your class. If you have this value as class property you need to keeping eye on it every time you access this value but if you retrieving it from cache you just don't care (because the cache cares if value of some key doesn't changed or expired).

Calling a parent classes method vs. calling a static method

To call a parent class (it has been instantiated) method I use
parent_class::method(); //tested it works
to call a method with in the instantiated class I am in I use
$this->method(); //tested it works
However if I call a static method from any class I use
parent_class::static_method(); //tested it works
I guess this makes since b.c. there is only one copy of a method per class, whether it is instantiated or not?
Can some one validate or provide insight to this. I just want to verify that the call method is the same for both static methods from any class and calls to a parent classes method.
Seems a bit strange.
The syntax is correct. Not exactly sure what your question is. If you wanted to call a static method defined in the child from within the child, you could use self::static_method() or $this->static_method(). Either would work.

PHP class/method type inheritance setting the class as static or setting all the containing methods as static

If I wanted to have a class of static functions, would it be smarter to define the class as static and let the methods inherit that or define each method as static without setting a type for the class. My goal is to have a class of data type validation functions that I can call without having to create an instance of the containing class. Which method would be the better option for my intended purpose?
PHP doesn't support static classes. you must declare methods you need as static.
in php there is no such thing as static class.
use static functions or regular function. same thing

difference between object and static methods

What's the difference between static and object methods? Where and why are they used differently? When do I use which one of those?
With object methods you need to instantiate the class in order to use the method so say Bark is an object method
Dog myDog = new Dog();
myDog.Bark();
But now let's say Bark was a static method. I could just do:
Dog.Bark();
So a static method works on a class, not on an object.
Static methods are useful when you'd like to just make a global utility class. That way you don't need to pass an object around just to use methods on this utility class.
static methods are instantiated only once in the memory space.
Instance methods require an instance of the class to be invoked. The instance reference can be thought of as an invisible first parameter, which can be accessed within the method using the 'this' keyword in C#, C++, and Java. Static methods can be invoked without an instance of the class. They can only access instances of the class if they are passed in as parameters.
As a general rule of thumb, use an instance method when the method performs some operation on a single instance. Use a static method when the method performs an operation on multiple instances, or requires no instances.
PHP manual is very brief about that. But static is explained quite well in the book "PHP 5 Power Programming":
Static properties
Static methods
Singleton pattern (scroll down to the singleton section there)

Categories