Wednesday, 8 January 2020

Why shouldn't a class parentage function have a global $$Obj statement

In the php.net manual for Class/Objects examples this highly downvoted comment was left

In order to avoid an "Undefined variable" error, the class_parentage fucntion should have the global $$obj stattement :

function class_parentage($obj, $class) {
        global $$obj;
        if (is_subclass_of($GLOBALS[$obj], $class)) {
            echo "L'objet $obj appartient à la classe " . get_class($$obj);
            echo " une sous-classe de $class\n";
        } else {
            echo "L'object $obj n'appartient pas à une sous-classe $class\n";
        }
    }

Please could someone explain why this is bad advice or why the downvoters are wrong.



from Why shouldn't a class parentage function have a global $$Obj statement

No comments:

Post a Comment