I found following code on the Internet:

class A {
    protected  function __construct() {
        echo __CLASS__;
    public static function getInstance() {
        $s= new static();

class B extends A {
    protected function __construct() {
        echo __CLASS__;

class C extends B {
    protected function __construct() {
        echo __CLASS__;


I modified it a little and it works as expected. My classes can be singleton by inheriting from first class A.
But I don't understand '$s= new static();' expression in that class. It is not how PHP manual suggests.

Can somebody explain it to me and/or provide link to the place where it come from?

Thanks in advance

4 Years
Discussion Span
Last Post by veedeoo


But I don't understand '$s= new static();' expression in that class. It is not how PHP manual suggests.

I test out your code and it works! I'm learning OOP by myself also.

I'm bit curious where did you find this example?

Regarding about '$s= new static();

Here is a couple of link that will explain more in detail about: '$s= new static();

Read this:


and read this


Both links explain the static methods in details.

I think you already read this



Just to add some simple ensight, the $s don't even matter it is merely a place holder, because it is there waiting for whenever the class is called upon. So, we can replace the $s = new static with

   return new static();

this class will still continue to return the instances of the classes ABC respectively, because of the hierarchy imposed on class B inherit class A and ultimately class C inherits class B.

Let us get out of the OOP conundrum for a just a few seconds, and lets take few steps back. Remember in basic PHP how we write functions? kind of something like this

    function SaySomething(){


        echo 'Form submitted';



What is happening in the simple codes above is just waiting for the right event when form is submitted.. therefore we can place this code anywhere in the page where the processor is located.

    $s = SaySomething();

    ## or just simply use code below and the effect and response will be absulutely the same

Pretty much that if ever the SaySomething is going to be triggered by the submit button, 'Form submitted' should be showing on the page, the same is true with the $s in your class.

If the class does not have any child classes, then the class can be written like this

    class A{

        public static $instanceA = NULL;

        private function __construct(){

            ## echo ONLY for the purpose of checking the instance of this class.NEVER echo something from the constructor.. that makes it look bad.

            echo 'This is an instance of A';


        public static function getInstanceOf_A() {
            if(!isset(self::$instanceA)) {
              self::$instanceA = new A();
            return self::$instanceA;


         public function SaySomething(){
             ## once again echoing something from within the method of a class should be avoided. There are many other means and techniques to isolate text string and to keep them outside. This will be the exemption just to bring my points out.

             echo 'Hi! My name is Neo';



To use the class A we can write our codes like this

    $s = A::getInstanceOf_A();

    ## or we can chain them like this (NOT MY WAY of chaining, BUT it MAY NOT WORK )
    $ = A::getInstanceOf_A()->function SaySomething();

    ## or the simplest form can just be as (NOT MY WAY of chaining, BUT it MAY NOT WORK )
    A::getInstanceOf_A()->function SaySomething();

    ## or

The second example above gives us this

    return self::$instanceA;

That is the equivalent ( not literal but rather symbolic ) of

    return new static();       

More so, if Class A is needing some method or methods from class B and C (these are not child classes ), then the instance of B and C can be called upon within the constructor of A. Like ...

    private function __construct(){
            $thisClassB-> = B::getInstanceOf_B();
            $thisClassC-> = C::getInstanceOf_C();


        public function SomeMethodFromB_C(){

                return array($thisClassB->saySomethingFromB(), $thisClassC->saySomethingFromC());


I hope did not add more confusion into this matter..

Edited by veedeoo: more info. added

Votes + Comments
Nicely Explain!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.