Some classes are better left un-instantiated, but PHP4 provides no direct way to to this…
To emulate a non-instantiable class (it can be worked around if someone is hell bent on instantiating it) we have to make a constructor that will refuse an instantiation attempt…
This can simply be achieved by :
class myClass { function myClass() { $this = null; trigger_error( sprintf("'%s class is un-instantiable!'", __CLASS__), E_USER_ERROR); } }
This kills the instance during instantiation, however it will also kill any derived classes with no constructor…
Easily fixed
class myClass { function myClass() { if(get_class($this)==__CLASS__) { $this = null; trigger_error( sprintf("'%s class is un-instantiable!'", __CLASS__), E_USER_ERROR); } } }
Now this code will check if the current instance is the same type as the base class. If so then it kills the instance, otherwise the instantiation is allowed
Hope this is of use to someone 🙂