2

I have reviewed PHP magic methods but don't see any way at least from these to accomplish this.

I would like to have a method "magically" called when any class is instantiated, something like __onClassCall(), which is not specifically written in the class - this would be for debugging and I would like to turn it on or off. Similarly, I would like to fire another method when any method in a class is called, which again could be written somewhere else and turned on or off. Is this possible?

Oliver Williams
  • 5,966
  • 7
  • 36
  • 78
  • Did you try the `Reflection`? Have a look at [ReflectionMethod::invoke](http://php.net/manual/en/reflectionmethod.invoke.php) – postrel Jun 25 '16 at 12:36
  • Search for *debug backtrace* - it might be what you're looking for. For example: http://stackoverflow.com/a/8497530/2329487 – shudder Jun 25 '16 at 16:42
  • You could use aop framework: https://github.com/goaop/framework – sectus Jul 04 '16 at 05:41

1 Answers1

0

You may want to implement those on your own. The __invoke() Magic Method might not be what you need as you still have to explicitly trigger it, anyways. Consider the following code below where you have a MagicHelper Class wherein you declare your Magic Methods and then use them within your Class (Here: SomeClassOne). This might not be a built-in PHP Function but that is what OOP is all about, right?

    <?php

        class MagicHelper{

            public static function __onClassCall(){
                // LOGIC FOR SOME SPECIAL INITIALIZATION ON THE INSTANTIATION OF A CLASS OBJECT GOES HERE...
                // THIS METHOD IS FIRED EACH TIME AN OBJECT IS INSTANTIATED
            }

            public static function __onMethodCall(){
                // LOGIC FOR DEBUGGING GOES HERE...
                // THIS METHOD IS FIRED EACH TIME A METHOD IS CALLED
            }
        }


        class SomeClassOne{
            private $prop;

            public static $CLASS_PREP_ENABLED       = true; //<== COULD BE TURNED OFF OR ON
            public static $METHOD_DEBUG_ENABLED     = true; //<== COULD BE TURNED OFF OR ON

            public function __construct($prop = null) {
                $this->prop     = $prop;

                if(self::$CLASS_PREP_ENABLED){
                    // CALL THE STATIC MAGIC __onClassCall METHOD DECLARED IN THE MagicHelper CLASS
                    // EACH TIME A NEW OBJECT IS INSTANTIATED
                    MagicHelper::__onClassCall();
                }
            }

            function __invoke() {
                // YOU COULD AS WELL USE THIS BUT I DOUBT THIS FITS YOUR DESCRIPTION.
            }

            public function getProp() {
                return $this->prop;
            }

            public function setProp($prop) {
                $this->prop = $prop;
                return $this;
            }

            public function methodOne($paramOne){
                // THE LOGIC FOR THIS METHOD GOES HERE
                //...
                // CALL THE STATIC MAGIC __onMethodCall METHOD DECLARED IN THE MagicHelper CLASS
                // EACH TIME A NEW OBJECT IS INSTANTIATED
                if(self::$METHOD_DEBUG_ENABLED) {
                    MagicHelper::__onMethodCall();
                }

            }

            public function methodTwo($paramTwo){
                // THE LOGIC FOR THIS METHOD GOES HERE
                //...
                // CALL THE STATIC MAGIC __onMethodCall METHOD DECLARED IN THE MagicHelper CLASS
                // EACH TIME A NEW OBJECT IS INSTANTIATED
                if(self::$METHOD_DEBUG_ENABLED) {
                    MagicHelper::__onMethodCall();
                }
            }
        }
Poiz
  • 7,611
  • 2
  • 15
  • 17