简体   繁体   English

让父母在PHP中扩展类

[英]get parent extends class in php

i have the oop php code: 我有oop php代码:

class a {
    // with properties and functions
}

class b extends a {
    public function test() {
        echo __CLASS__; // this is b
        // parent::__CLASS__ // error
    }
}

$b = new b();
$b->test();

I have a few parent class (normal and abstract) and many child classes. 我有一些父类(普通和抽象)和许多子类。 The child classes extend the parent classes. 子类扩展了父类。 So when I instantiate the child at some point I need to find out what parent I called. 因此,当我在某个时候实例化孩子时,我需要找出我叫什么父母。

for example the function b::test() will return a 例如,函数b::test()将返回a

How can I get (from my code) the class a from my class b? 我怎样才能获得(从我的代码)类a从我的b类?

thanks 谢谢

Your code suggested you used parent , which in fact is what you need. 您的代码建议您使用parent ,这实际上是您所需要的。 The issue lies with the magic __CLASS__ variable. 问题在于神奇的__CLASS__变量。

The documentation states: 文档指出:

As of PHP 5 this constant returns the class name as it was declared. 从PHP 5开始,此常量返回声明的类名称。

Which is what we need, but as noted in this comment on php.net: 这就是我们所需要的,但是正如在php.net上的注释中所指出的:

claude noted that __CLASS__ always contains the class that it is called in, if you would rather have the class that called the method use get_class($this) instead. claude指出__CLASS__始终包含被调用的类,如果您希望让调用该方法的类使用get_class($ this)代替。 However this only works with instances, not when called statically. 但是,这仅适用于实例,不适用于静态调用。

If you only are in need of the parent class, theres a function for that aswell. 如果只需要父类,那么还有一个函数。 That one is called get_parent_class 那个叫get_parent_class

You can use get_parent_class : 您可以使用get_parent_class

class A {}
class B extends A {
  public function test() {
    echo get_parent_class();
  }
}

$b = new B;
$b->test(); // A

This will also work if B::test is static. 如果B::test是静态的,这也将起作用。

NOTE: There is a small difference between using get_parent_class without arguments versus passing $this as an argument. 注意:使用不带参数的get_parent_class与将$this作为参数传递之间存在细微的差别。 If we extend the above example with: 如果将上述示例扩展为:

class C extends B {}

$c = new C;
$c->test(); // A

We get A as the parent class (the parent class of B, where the method is called). 我们将A作为父类(B的父类,在其中调用该方法)。 If you always want the closest parent for the object you're testing you should use get_parent_class($this) instead. 如果您始终想要您要测试的对象的最接近的父对象,则应改用get_parent_class($this)

class a {
  // with propertie and functions
}

class b extends a {

   public function test() {
      echo get_parent_class($this);
   }
}


$b = new b();
$b->test();

You can use reflection to do that: 您可以使用反射来做到这一点:

Instead of 代替

parent::__CLASS__;

use 使用

$ref = new ReflectionClass($this);
echo $ref->getParentClass()->getName();

Use class_parents instead. 请改用class_parents It'll give an array of parents. 它会给很多父母。

<?php
class A {}
class B extends A {
}
class C extends B {
    public function test() {
        echo implode(class_parents(__CLASS__),' -> ');
    }
}

$c = new C;
$c->test(); // B -> A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM