Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class extending itself?

Tags:

syntax

oop

php

I encountered a PHP class written by a previous employee that is defined like this:

namespace SomeNamespace;

class SomeClass extends ::SomeClass
{
    private function __construct() {}

    public static function someFunction()
    {
        //Do something
    }
}

Would somebody please explain what is going on here? Is this class extending itself? I know a singleton would use a private constructor, but not sure that is what is happening here.

The actual class has to do with caching. Not sure if that helps.

like image 561
Mike Moore Avatar asked Jul 20 '11 19:07

Mike Moore


People also ask

Can a class extend itself?

A class cannot extend itself since it IS itself, so it is not a subclass. Inner classes are allowed to extend the outer class because those are two different classes.

What is the meaning of extending class?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

What is it called when a class extends another class?

Class inheritance is a way for one class to extend another class.

Can an abstract class extend itself?

Yes abstract class does extends the object class. And it inherits properties of object class.


2 Answers

I don't think you can do this kind of thing using :: ; but it should work if you are using the namespace separator : \

I just tested with PHP PHP 5.3.5 -- and using :: does indeed get me an error.
Using \, on the other hand, works just fine (see example below).


For example, being in the global namespace (outside of any specific namespace) you could define a class SomeClass :

namespace {

    class SomeClass
    {
        public function method() {
            var_dump("child: " . __METHOD__);
        }
    }


    $obj = new SomeNamespace\SomeClass();
    $obj->method();

}


And, in a specific namespace called SomeNamespace in my example, you could define a class SomeClass that would extend the class from the global namespace -- which is accessible as \SomeClass :

namespace SomeNamespace {

    class SomeClass extends \SomeClass
    {
        public function method() {
            var_dump("parent: " . __METHOD__);
        }
    }

}


Sidenote : Here, I've use the bracketed syntax, as I'm putting the two portions of code in the same file (see Example 3 on that manual page) -- but it should work just the same with several files.



Note that with the first alpha versions of PHP 5.3, the namespace separator was not \, but ::

So, if you are using PHP 5.3 alpha 1 or 2 (and maybe 3, not sure if \ has been choosen before or after alpha 3), using :: might work.

(Of course, you should not be using an alpha -- especially considering that there have been stable versions of PHP 5.3 for two years now)

like image 160
Pascal MARTIN Avatar answered Oct 12 '22 23:10

Pascal MARTIN


If I'm remembering correctly during the development of 5.3 :: was used as the namespace separator.

like image 33
Glass Robot Avatar answered Oct 12 '22 23:10

Glass Robot