Jump to Content. Netsuite Zend Framework. Apache Perl. Finances Passive Income. Multiple Constructors in PHP. Add new comment. By anomyous not verified. Like constructors, parent destructors will not be called implicitly by the engine.
Also like constructors, a child class may inherit the parent's destructor if it does not implement one itself. The destructor will be called even if script execution is stopped using exit. Calling exit in a destructor will prevent the remaining shutdown routines from executing. The working directory in the script shutdown phase can be different with some SAPIs e.
Note : Attempting to throw an exception from a destructor called in the time of script termination causes a fatal error. Visibility ». Submit a Pull Request Report a Bug. Old-style constructors Prior to PHP 8. Static creation methods PHP only supports a single constructor per class. Be aware of potential memory leaks caused by circular references within objects.
The programmer can no longer access the objects, but they still stay in memory. Note: In PHP 5. I spent quite a bit of needless time debugging working code. I even thought about it a few times, thinking it looked a little long in the examples, but at the time that just seemed silly always thinking "oh somebody would have made that clear if it weren't just a regular underscore I hope this helps somebody else! As of PHP 5. Removing the unset will fix this.
It's not needed anyways as PHP will release everything anyways, but just in case you run across this, you know why ;. Ensuring that instance of some class will be available in destructor of some other class is easy: just keep a reference to that instance in this other class.
Peter has suggested using static methods to compensate for unavailability of multiple constructors in PHP. This is not exactly what you are looking for, but a simplistic trait based approach would be:. We end up with two classes, one for each constructor, which is a bit counter-productive. To maintain some sanity, I'll throw in a factory:. Here is an elegant way to do it. Create trait that will enable multiple constructors given the number of parameters.
On the other hand, if you want to make use of polymorphism then you can create two classes like so:. I know I'm super late to the party here, but I came up with a fairly flexible pattern that should allow some really interesting and versatile implementations. This question has already been answered with very smart ways to fulfil the requirement but I am wondering why not take a step back and ask the basic question of why do we need a class with two constructors?
If my class needs two constructors then probably the way I am designing my classes needs little more consideration to come up with a design that is cleaner and more testable. If a Student object is in a valid state, then does it matter if it was constructed from the row of a DB or data from a web form or a cli request? Now to answer the question that that may arise here that if we don't add the logic of creating an object from db row, then how do we create an object from the db data, we can simply add another class, call it StudentMapper if you are comfortable with data mapper pattern, in some cases you can use StudentRepository, and if nothing fits your needs you can make a StudentFactory to handle all kinds of object construction tasks.
Bottomline is to keep persistence layer out of our head when we are working on the domain objects. I personally like adding a constructors as static functions that return an instance of the class the object. The following code is an example:.
It will look at constructor parameter types array, class name, no description and compare the given arguments. Constructors must be given with least specificity last. With examples:. Instead of the terminating exception if no constructor is found, it could be remove and allow for "empty" constructor.
Or whatever you like. For php7, I compare parameters type as well, you can have two constructors with same number of parameters but different type. So for your case you should have 2 classes:. Also please note that you should use doctrine or other ORM that already provides automatic entity hydration. And you should use dependency injection in order to skip mannualy creating objects like StudentHydrator. In response to the best answer by Kris which amazingly helped design my own class btw , here is a modified version for those that might find it useful.
Includes methods for selecting from any column and dumping object data from array. You could always add an extra parameter to the constructor called something like mode and then perform a switch statement on it Also with that method at any time if you wanted to add more functionality you can just add another case to the switch statement, and you can also check to make sure someone has sent the right thing through - in the above example all the data is ok except for C as that is set to "something" and so the error flag in the class is set and control is returned back to the main program for it to decide what to do next in the example I just told it to exit with an error message "invalid mode" - but alternatively you could loop it back round until valid data is found.
I was facing the same issue on creating multiple constructors with different signatures but unfortunately, PHP doesn't offer a direct method to do so. Howerever, I found a trick to overcome that. Hope works for all of you too. I also found an useful example in php. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 12 years ago. Active 2 months ago.
Viewed k times. Improve this question. Jannie Theunissen Jannie Theunissen In my case, I want to have a protected constructor that has one less required argument than the public one - for the sake of standardizing its factory method. I need a class to be able to create copies of itself, and the factory is in an abstract class, but the concrete classes may have constructors that require a second argument - which the abstract class has no idea of.
But I do not know what PHP does internally with it. Add a comment. Active Oldest Votes. Improve this answer.
0コメント