$my_object->finish() // Show what our variables are $my_object->set_three(6) // Set three to something (since it's not set in the constructor) $my_object = new M圜lass() // Create an instance of the object I added this method to the M圜lass class:Ĭhanged the create method (again) in the M圜lass class to this: However, I couldn't help but wonder: Does the existing object's constructor get called, returning the existing object, or is the object's type the only portion that is used in the call to new? Printf('_one: %d, _two: %d, _three: %d', $this->_one, $this->_two, $this->_three) Īs mentioned on Twitter, the same effect can be achieved by changing the create function to:Īfter seeing this, I understood that new could take an existing object as an argument. So, let's create the awesome M圜lass and give it a static function that will return a chainable object: In addition to PHP variables, static method calls can also be chained off of (surprisingly enough). The solution is found when you realize that you need to replace "new M圜lass" with something that can be chained off of. No extra baggage needed, right?įor times like those, you'd like to use something like: The second solution has a bit of brief research to go along with it! :-)Īt times, it can be useful to have a class that maintains state long enough to complete a cycle, but doesn't get stored in memory. Note: Make sure you read/skim to the end, even if you figure out the first solution quickly (which you should).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |