简体   繁体   中英

Is it possible to get the name of class/file which extends another class in PHP?

I have a PHP website which has two core php files ci_controller.php and my_controller.php and a number of controller php files. My_Controller class extends CI_Controller . The other contoroller PHP classes extend My_Controller .

I was wanting to change the website so that some of the thing My_Controller class will only occur for some of the classes which extended. ie depending on which class extended My_Controller do x.

I was wondering if it is possible to use the parent/child relationship (or see which files extend a class) to create an if/exception so that some of the My_Controller parts on run for certain PHPs?

For example if a three classes;

  • classA, classB and classC.
  • classA and classB both extend classC (ie class classA extends classC)

In this example I want to have a line in classC which does the following pseudo code:

if (extended by classA)
{//do something}
else if (extended by classB)
{//do something else}

NOTE: Two similar questions on stackoverflow are:

However they do not solve my problem, but maybe helpful for those looking for a similar solution

PHP has a function that will check if a class is a subclass of another.

http://php.net/manual/en/function.is-subclass-of.php

The following is taken from the docs:

<?php
// define a class
class WidgetFactory
{
  var $oink = 'moo';
}

// define a child class
class WidgetFactory_Child extends WidgetFactory
{
  var $oink = 'oink';
}

// create a new object
$WF = new WidgetFactory();
$WFC = new WidgetFactory_Child();

if (is_subclass_of($WFC, 'WidgetFactory')) {
  echo "yes, \$WFC is a subclass of WidgetFactory\n";
} else {
  echo "no, \$WFC is not a subclass of WidgetFactory\n";
}


if (is_subclass_of($WF, 'WidgetFactory')) {
  echo "yes, \$WF is a subclass of WidgetFactory\n";
} else {
  echo "no, \$WF is not a subclass of WidgetFactory\n";
}


// usable only since PHP 5.0.3
if (is_subclass_of('WidgetFactory_Child', 'WidgetFactory')) {
  echo "yes, WidgetFactory_Child is a subclass of WidgetFactory\n";
} else {
  echo "no, WidgetFactory_Child is not a subclass of WidgetFactory\n";
}
?>

Output:

yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory

On another page there is a user-contributed note that has a function that gets the children (if you want to go the other way at some point):

<?php
function get_child($instance, $classname) {
    $class = $classname;
    $t = get_class($instance);
    while (($p = get_parent_class($t)) !== false) {
        if ($p == $class) {
            return $t;
        }
        $t = $p;
    }
    return false;
}

abstract class A {
    function someFunction() {
        return get_child($this, __CLASS__);
    }
}

class B extends A {

}

class C extends B {

}

$c = new C();
echo $c->someFunction(); //displays B

?>

Very simple:

class MyController {

    public function someFunc() {
        if ($this instanceof ClassA || $this instanceof ClassB) {
            ...
        }
    }

}

BUT: This is a horrible design. A parent should not know about its children and specialise its behaviour depending on who extends it. It needs to be the other way around. The parent provides some functionality and the children each use it as necessary. Turn it around and only call a parent function from a specific child , and don't call it from other children:

class MyController {
    public function foo() { .. }
}

class ClassB extends MyController {
    public function __construct() {
        $this->foo();
    }
}

class ClassD extends MyController {
    public function __construct() {
        // simply don't call
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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