Get child class namespace from superclass in PHP

后端 未结 6 1064
南旧
南旧 2020-12-09 02:24

Assuming I have the following classes in different files:



        
6条回答
  •  难免孤独
    2020-12-09 03:01

    Hope this helps.

    /* First Namespace */
    namespace MyNS {
        class superclass {
            /* Functions to get the current namespace
             *  If $object is null then return the
             *  namespace of the class where the
             *  method exists, if not null, return
             *  the namespace of the class called.
             */
            public static function get_namespace($object = null) {
                if($object !== null) {
                    $tmp = (($object != "self") && (get_called_class() != get_class($object))) ? get_class($object) : get_called_class();
                    $tmparr = explode("\\", $tmp);
                    $class = array_pop($tmparr);
                    return join("\\", $tmparr);
                } else {
                    return __NAMESPACE__;
                }
            }
            public static function get_current_namespace() {
                return self::get_namespace(self);
            }
    
            public function call_static_method($class_name, $method_name, $arguments = array()) {
                $class = "\\" . $this->get_namespace($this) . "\\{$class_name}";
                if(method_exists($class, $method_name)) {
                    if(count($arguments) > 0) return $class::$method_name($arguments);
                    return $class::$method_name();
                }
                return "Method ({$method_name}) Does not exist in class ({$class})";
            }
    
            public function call_user_method($object, $method_name, $arguments = array()) {
                if(method_exists($object, $method_name)) {
                    if(count($arguments) > 0) return $object->$method_name($arguments);
                    return $object->$method_name();
                }
            }
        }
    
        class superclass2 extends superclass {
            public static function foo() {
                return "superclass2 foo";
            }
            public function bar() {
                return "superclass2 bar";
            }
        }
    }
    
    /* Second Namespace */
    namespace MyNS\SubNS {
        class childclass extends \MyNS\superclass { }
    
        class childclass2 extends \MyNS\superclass {
            public static function foo() {
                return "childclass2 foo";
            }
            public function bar() {
                return "childclass2 bar";
            }
        }
    }
    
    /* Back to Root Namespace */
    namespace {
        /* Returns 'MyNS' */
        echo \MyNS\superclass::get_namespace() . "
    "; echo \MyNS\SubNS\childclass::get_namespace() . "
    "; /* Returns 'MyNS' */ echo \MyNS\superclass::get_current_namespace() . "
    "; /* Returns 'MyNS\SubNS' */ echo \MyNS\SubNS\childclass::get_current_namespace() . "
    "; /* Or this way */ $super = new \MyNS\superclass(); $child = new \MyNS\SubNS\childclass(); /* Returns 'MyNS' */ echo $super->get_namespace() . "
    "; echo $child->get_namespace() . "
    "; /* Returns 'MyNS' */ echo $super->get_namespace($super) . "
    "; /* Returns 'MyNS\SubNS' */ echo $child->get_namespace($child) . "
    "; /* Returns 'superclass2 foo' */ echo $super->call_static_method("superclass2", "foo") . "
    "; /* Returns 'superclass2 bar' */ $super2 = new \MyNS\superclass2(); echo $super->call_user_method($super2, "bar") . "
    "; /* Returns 'superclass2 foo' */ echo $child->call_static_method("childclass2", "foo") . "
    "; /* Returns 'superclass2 bar' */ $child2 = new \MyNS\SubNS\childclass2(); echo $child->call_user_method($child2, "bar") . "
    "; }

    Edited in response to Artur Bodera to add 'call' functionality

提交回复
热议问题