Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute (magic) method when existing method is called

Tags:

php

Is there a magic method that when a certain method is called from an object, that a magic method is called first. Kinda like the __call method, but this only gets triggered when the method isn't found.

So in my case i'd like something like this:

class MyClass
{
    public function __startMethod ( $method, $args )
    {
        // a method just got called, so  this is called first
        echo ' [start] ';
    }

    public function helloWorld ( )
    {
        echo ' [Hello] ';
    }
}

$obj = new MyClass();
$obj->helloWorld();

//Output:
[start] [Hello] 

Does something like this exist in PHP??

like image 329
w00 Avatar asked Apr 06 '12 18:04

w00


People also ask

Which magic method is called when a user calls an object as a function?

__invoke(): This method is defined in a class that will be called while trying to call an object in a way of calling function.

Which magic method is called when object of class is unset?

public __unset ( string $name ) : void. __unset() is invoked when unset() is used on inaccessible (protected or private) or non-existing properties.

What's the special meaning of __ sleep and __ wakeup?

__sleep is supposed to return an array of the names of all variables of an object that should be serialized. __wakeup in turn will be executed by unserialize if it is present in class. It's intention is to re-establish resources and other things that are needed to be initialized upon unserialization.


3 Answers

There isn't a direct way to do this but it looks to me like your trying to implement a form of Aspect Oriented programming. There are several ways of achieving this in PHP, one would be to set up your class something like the following:

class MyClass
{
    public function __startMethod ( $method, $args )
    {
        // a method just got called, so  this is called first
        echo ' [start] ';
    }

    public function _helloWorld ( )
    {
        echo ' [Hello] ';
    }

    public function __call($method, $args)
    {
        _startMethod($method, $args);
        $actualMethod = '_'.$method;
        call_user_func_array(array($this, $actualMethod), $args);
    }
}

$obj = new MyClass();
$obj->helloWorld();

Look up other ways of implementing AOP in PHP to see what works best for you (I'll see if I can find a link somewhere).

EDIT: here's a document for you http://www.liacs.nl/assets/Bachelorscripties/07-MFAPouw.pdf

like image 71
Godwin Avatar answered Sep 16 '22 12:09

Godwin


No there is no magic method for this.

The best you could do is create other names for your functions (eg: hidden_helloWorld), then catch all the calls with __call and try to call the hidden_ method if it is available. Of course this is only possible if you have full control over the naming of the class and its parent, etc...

like image 32
Karoly Horvath Avatar answered Sep 20 '22 12:09

Karoly Horvath


YOu could achieve it by making your methods private and calling the methods using the __call() magic method. Like:

<?php

class MyClass{
    function __call($methd, $args){
        if(method_exists($this, $mthd)){
            $this->$mthd($args);
        }
    }

    private function mthdRequired($args){
        //do something or return something
    }

The mthdRequired method doesnt get called except using call. I hope this is usefull.

like image 36
Sunday Ikpe Avatar answered Sep 20 '22 12:09

Sunday Ikpe