We’ve all been told to “just use a template” and follow one of the many recommended guidelines. But why? Aren’t those guidelines just making it harder for developers to work with PHP? The reality is that it’s not really that hard to extend a class, but there are a few reasons why it’s not a straightforward process.
An excellent one, so to speak. Thanks to the folks at the game’s developer, Michael, who helped us out here while we were on vacation.
So how do you extend a class? Good question. Thats the first thing that we need to know before we can get started. The easiest way to look at it is to think of it as a little program that extends a class. This is a really good overview of how a class can be extended and, more importantly, how to do that properly. There are three ways to extend a class: with a constructor, with a base class, or with an interface.
There are three ways to extend a class with a constructor. The first is to declare a constructor as our new class’ constructor. This is the best way to extend a class with a constructor because you don’t have to worry about the specifics of how to create the instance or how to call it. However, if you do this, you will lose the ability to create new instances of your class.
As it turns out, this is a serious problem with PHP 5.5+ because PHP 5.5+ requires all new classes to have a constructor. So if you already have a class that you want to extend with a constructor, you’ll have to be careful to declare it as a constructor on the class declaration. That was the case with php-extended. This is why I’m recommending you to use PHP 5.6 instead of 5.
Yes, this is a serious problem with PHP 5.5 because the constructor is the method that allows you to create new instances of your class. So if you use PHP 5.5, you can no longer create new instances of your class.
The problem is, if you use 5.5 you cannot ever extend your class with a constructor. So if you want to extend your class you have to create it as a non-constructor. That was the case for the php-exlended class. You can still extend it with a constructor, but the only way to create new instances of your class is to use the class. So you have to either extend your class or create it as a non-constructor.
Yeah that was the case for the php-exlended class. But now you can extend your class with a constructor. That was the case for the php-exlended class. But now you can extend your class with a constructor. That was the case for the php-exlended class. But now you can extend your class with a constructor. That was the case for the php-exlended class. But now you can extend your class with a constructor.
Now, PHP can be very handy in certain situations. I’ve used it many times to create a class that has an empty constructor and a setter that can set a property. It’s handy for creating classes that can be extended, where you don’t want to create a constructor for every member. The constructor you use now is a non-constructor. Technically, it is not a constructor because it is not creating a new object.
The php-exlended class uses the new keyword in the setter method to assign the extended class to any instance it instantiates. So you can extend the class with a setter and then use the new keyword in the constructor to set the property of the constructor. This makes it easier to extend classes with only one constructor.