6

this is my class:

class toyota extends car {
    function drive() {
    }
    function break() {
    }
}

class car {
    function pre() {
    }
}

Is there any way I can do so that when I run $car->drive(), $car->break() (or any other function in toyota), it would call $car->pre() first before calling the functions in toyota?

Patrick
  • 4,815
  • 11
  • 52
  • 55
  • Read up on "constructor" functions...that may be what you need. – JW. Mar 28 '12 at 04:38
  • Can you please clarify? I can't tell if you want to run $car->pre() whenever you create a new object, or anytime you call a method of $car. The answers below cover both - I just don't know which one to mod up. – Surreal Dreams Mar 28 '12 at 04:42

5 Answers5

14

Yep. You could use protected and some __call magic:

class toyota extends car {
    protected function drive() {
        echo "drive\n";
    }
    protected function dobreak() {
        echo "break\n";
    }
}

class car {
    public function __call($name, $args)
    {
        if (method_exists($this, $name)) {
            $this->pre();
            return call_user_func_array(array($this, $name), $args);
        }


    }

    function pre() {
        echo "pre\n";
    }
}

$car = new toyota();
$car->drive();
$car->dobreak();

http://ideone.com/SGi1g

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 1
    Not only is this the only answer that uses __call (the right way to do it) but it also is the only one that actually does some method_exists check and actually calls the originally called method to begin with. Do this! – sberry Mar 28 '12 at 04:46
  • Do you mean private instead of protected? Cause protected can still be visible by parent and inherited. – Patrick Mar 28 '12 at 04:50
  • @sberry, checking with method_exists() will hide the default error received when calling invalid methods – Starx Mar 28 '12 at 05:00
  • @Starx: yep, but in case of not existent method you could throw native error or exception – zerkms Mar 28 '12 at 07:42
  • @Patrick: nope, `private` won't work, because parent class doesn't have an access to the ancestor's private methods – zerkms Mar 28 '12 at 07:42
  • You're right. But I tried protect and it doesn't work too. It says: Fatal error: Call to protected method – Patrick Mar 28 '12 at 08:56
  • I think it doesn't work because __call only executed if the function doesn't exist. – Patrick Mar 28 '12 at 09:49
  • @Patrick: If you go to http://ideone.com/SGi1g you will see that it works. Most likely that you've done something wrong – zerkms Mar 28 '12 at 19:20
  • Some places it works but some it doesn't. This is the error it returns Fatal error: Call to protected method toyota::drive() from context ''. Any idea what that could mean? my php version is 5.2.8, is that the problem? – Patrick Mar 29 '12 at 04:51
1

You could do the following, but I don't think that is what you want.

class toyota extends car {
    function drive() {
         $this->pre();
    }
    function break() {
         $this->pre();
    }
}

class car {
    function pre() {
    }
}

You may want to look into PHP specific magic methods. http://php.net/manual/en/language.oop5.magic.php

Gohn67
  • 10,608
  • 2
  • 29
  • 35
0

If you are using PHP5 (>=5.3.2), there is a solution that works with declaring all methods as private. This will enforce method call from single function call:

exec_method()

To run at: http://ideone.com/cvfCXm

The code snippet is here:

<?php

    class car {

        //method to get class method
        public function get_method($method_name) {
            $class = new ReflectionClass(get_class($this));
            $method = $class->getMethod($method_name);
            $method->setAccessible(true);
            return $method;
        }

        public function exec_method($method_name, $arg_args=array()) {

            //execute the pre() function before the specified method
            $this->pre();

            //execute the specified method
            $this->get_method($method_name)->invokeArgs($this, $arg_args);
        }

        public function pre() {
            echo 'pre';
            echo '<br />';
        }
    }

    class toyota extends car {
        private function drive() {
            echo 'drive';
            echo '<br />';
        }

        private function brake() {
            echo 'brake';
            echo '<br />';
        }
    }

    $toyota = new toyota();
    $toyota->exec_method('drive');
    $toyota->exec_method('brake');
?>

Reference:

Answer to Best practices to test protected methods with PHPUnit [closed]

Community
  • 1
  • 1
Gabriel Chung
  • 1,527
  • 1
  • 18
  • 30
0

This will better done with the magic methods called __call()

public function __call($name, $arguments)
{
    $this -> pre();
    return $this -> $name($arguments);
}

What is this method? It overrides the default method call, so that preCall State can be invoked.

Your toyota class

class toyota extends car {    

    public function __call($name, $arguments)
    {
        $this -> pre();
        return call_user_func_array(array($this, $name), $arguments);
    }

    function drive() {
    }
    function break() {
    }
}
Starx
  • 77,474
  • 47
  • 185
  • 261
  • 1
    `$this -> $name($arguments)` --- will call the requested method with all the parameters passed as an array. So `$car->drive(1, 2)` will be re-called with `$car->drive(array(1, 2))` which is just incorrect – zerkms Mar 28 '12 at 07:46
-2

Just add a constructor, like this...

class toyota extends car {
    function __construct() { 
        $this->pre();
    }   

    function drive() {
        echo "drive!";
    }

    function dobreak() {
        echo "break!";
    }
}

class car {
    function pre() {
        echo "Hello!";
    }
}

$car = new toyota();
$car->drive();
$car->dobreak();

Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

break is reserved, so you shouldn't use this as a function name.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • i think he is looking for a way to run the method `pre` prior to a function call, not on object initialization – Malitta N Mar 28 '12 at 04:43
  • While replying I was interrupted. I've updated my example with the constructor. it still does the job. – Bjoern Mar 28 '12 at 06:24