How to get the name of child class from base class

2019-02-14 06:28发布

I want to get the name of my child class in the base class so that whenever an object of child class is created I get the name of the child class in my base class. Something like this:

class Base_class {
    function __construct() {
        // Some code Here to get the name of the child class
    }
}

class Child_class extends Base_Class {}

class Another_child_class extends Base_Class {}

$object = new Child_Class;
$object2 = new Another_child_class;

When the $object is created I want the constructor to give me the name of the class from which the object was created.

标签: php oop
4条回答
别忘想泡老子
2楼-- · 2019-02-14 07:08

You can use get_class() passing it the current object reference, like so:

class Base_class {

    function __construct() {
        $calledClassName = get_class($this);
    }

}

Edit: You can find more info on get_class() in the PHP manual http://php.net/manual/en/function.get-class.php

查看更多
放我归山
3楼-- · 2019-02-14 07:09

Yes, to an extent. In PHP 5.5, the ::class class constant was added, which returns the class name of class to which it is applied. You can then use this in conjunction with parent, self, or static. Consider this code:

<?php

class A {}

class B extends A
{
    public function foo() {
        echo parent::class . "\n";    // A
        echo __CLASS__ . "\n";        // B
        echo self::class . "\n";      // B
        echo static::class . "\n";    // D
        echo get_class($this) . "\n"; // D
    }
}

class C extends B {}
class D extends C {}

(new D)->foo();

self::class will return the same thing as __CLASS__, the class belonging to the function currently executing (in this case, B).

parent::class will return the name of the method's parent class (A).

static::class, by way of Late Static Binding, will return the class name of the method that was actually invoked (D).

Note, however, that you can not get the immediate child descendent of your current function (C) without using more advanced means (parsing the call stack via debug_backtrace, or via reflection).

查看更多
叛逆
4楼-- · 2019-02-14 07:11

Well, just pass it explicitly then:

class BaseClass{

  function __construct($tableName){
    // do stuff w/ $tableName
  }

}

class ChildClass extends BaseClass{

  function __construct(){
    parent::__construct('ChildClass');
  }

}
查看更多
神经病院院长
5楼-- · 2019-02-14 07:13

This is certainly possible using static::class (or get_class($this) or get_called_class()) in the base class to get the name of the child (which is initially called at runtime):

<?php

class Foo 
{

    public function __construct()
    {
        var_dump(static::class, get_class($this), get_called_class());
    }

}

class Bar extends Foo { }

class Baz extends Bar { }

new Foo();
new Bar();
new Baz();

Produces:

string(3) "Foo"
string(3) "Foo"
string(3) "Foo"
string(3) "Bar"
string(3) "Bar"
string(3) "Bar"
string(3) "Baz"
string(3) "Baz"
string(3) "Baz"

This is called late static binding. Here's a demo of the above.

查看更多
登录 后发表回答