I'm trying to work out how to best complete my design work on my classes.
my situation.
i have an order abstract class that contains order methods and information that are required for 2 children classes
order_Outbound
and order_inbound
each child class requires 2 static public method called create and get
but from what i have read about php 5.3 you cant have abstract static methods ???
so my thought was to have an interface Order_Interface which takes over that role but how do i implement it. do i still implement it in the parent class
in which case the parent abstract class still requires me to create a get and create method within the abstract class. or do i implement it in the children and extend from the abstract class???
ALSO!!! both the outbound and inbound children require a create static method but require different parameters to be passed
can i in the interface have public static function create()
and in its implementation within order_outbound declare it public static function create($address, $reference, $orderID)
In most languages, including PHP, you cannot require a class to implement static methods.
This means neither class inheritance, nor interfaces, will allow you to require all implementors define a static method. This is probably because these features are designed to support polymorphism rather than type definition. In the case of static methods you'll never have an object to resolve the type from, so would have to do ClassName::Method
explicitly, so the theory is you wouldn't gain anything from polymorphism.
As such, I see three solutions
Declaring the static methods in each class (after all, you are never going to
If you want a method to create instances of your class, but don't want to require an instance to call this method, you could create "Builder" classes to serve this purpose (e.g. OrderBuilder
), such that you instantiate an OrderBuilder
and call the Create
method on this object instead to get Order
instances.
(Recommended) Why aren't you simply using the Order
constructor?
Update
After the comment from @hvertous, I decided to test this out. Using 3v4l we can see that abstract public static method
:
Which confirms that it was removed in PHP 5.2, but if you are using PHP 7+ you can once again use abstract static methods.
Original answer
Yes, abstract static methods were removed in PHP 5.2. Apparently they were an oversight. See Why does PHP 5.2+ disallow abstract static class methods?.
However, you can have static methods in an interface, see this comment on php.net.
The problem you face is that you want your implementations to have different function signatures, which means that you probably shouldn't be using inheritance to solve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With