I can make infinite calling by returning $this in class body functions after creating as object.
Is it possible to make the same without creating object?
Something like myclass::func1()::func2()::func3()
i was trying with reflection and stored new class in parent variable, but it returning also object, which couldnt be called by ::
No, it is not possible to chain static methods.
Related
I want to research the source code of pyrocms, and when I read the Base.php, I can't understand the following code
new CI;
the file is system/cms/libraries/Base.php
My problems are
why there has no a variable name, like $CI = new CI;
why it can be used as CI::$APP->config->item('controller_suffix') in it's sub class MX_Controller since there does not have variable name?
Thank you very much!!!
This object isn't stored in a variable because it seems we don't need to manipulate it. On the other hand, look at its constructor: it does a lot of things (since it also calls the constructor of CI_Controller, which in turns loads a Loader and initializer it, ....)
So, we don't build it in order to manipulate it afterwards, but in order to run the code in its constructor.
We can use CI::$APP-> whatever because $APP is a static member, hence it doesn't require to have an instance of CI to be manipulated
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).
see statics on php.net
I don't usually call methods statically but since working with the Yii framework I have started to more.
I'm experiencing an issue which I haven't come across before, I am doing this from a SignupForm class:
$send = mail::sendMail($email_data);
..inside the sendMail method which is obviously inside the mail class I have this line:
$email_data['message'] = $this->sanitizeMsg($email_data['message']);
The sanitizeMsg is a method of the mail class and hence that's why I thought referencing it via $this should work.
However I am getting the error:
Calling unknown method: app\models\SignupForm::sanitizeMsg()
Why is it looking for it in the SignupForm class? Does this have something to do with me calling it statically? Do I need to revert to using self:: instead or should I stop calling it statically?
Using $this only works for member methods. I.e. you can only use it from an instance of the class and I believe also only to access instance members.
To access static members, you should use self::.
Not familiar with the Yii framework, but what someone does is to use static methods as factory methods. Maybe that's what you're confused by. The static method then creates a new instance of itself and returns it, and inside that instance you can of course use $this, but not in the static method. Static methods have no $this.
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.
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 is probably a noob question, so please be kind.
I'm trying to implement a cache on an expensive "activity" object. In the constructor I first check the cache to see if this Activity instance already exists. If not, I do all the queries to build up the object, serialize it and save it to cache. The next time I come in, I check the cache and my object is there, so I unserialize it. Now is my problem, how do I put that object into $this, the current object? I can't just say "$this = unserialize($row[0]);" That fails with the error message, "Cannot re-assign $this in ActivityClass.php". What am I missing?
Thanks a ton!
Mike
If you don't want your construction to leave the class, you can create a factory method:
class Activity
{
public static function Create(/* your params */)
{
// construct cache and key, whatever
$obj = unserialize($cache->get($key));
if ($obj) return $obj;
return new Activity(/* params */);
}
// rest of your stuff
}
You'll have to serialize only your object's internal state, i.e. its parameters (aka "member variables"). In fact, in this instance, serialize() isn't really what you want to do; rather, you want to store your ActivityClass's data to your cache, not the serialization of the entire object. This gets tricky, though, because as you add new parameters later you need to remember to store these in your cache as well.
Alternatively, you can implement a singleton or factory pattern for your ActivityClass. Since you say you're pulling the class from the cache in the constructor, I take it that only one instance of this class is meant to exist at any given time? In this case, you should make your class a singleton, by doing the following:
Make the __construct() method private or protected.
Create a public static method (I tend to call this getInstance()) that will check your cache for the object, or instantiate a new one and then cache it.
Now instead of directly instantiating a new ActivityClass object, you instead write $foo = ActivityClass::getInstance();, which gives you either a new object or unserializes and returns your cached one.
As you noticed, you cannot just override the current object as a whole.
Instead, a possibility would be to store the data you're serializing/unserializing into a property of your object.
This way, you wouldn't serialize your whole object, but only one of its properties -- and only that single property would be overriden when unserializing.
Typically, you wouldn't serialize the connection to the database, which could be another property of your object.
Another possibility would be to not have your object deal with its own (de-)serialization.
Instead, you should :
Use an external class to instanciate your object
With that external class being responsible of either :
Loading data from cache and pushing it into your object,
Or calling the right method of your class, to load data from the database -- and, then, save that object to cache.