Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function like the magic method __call() for global scope in php?

Tags:

php

If a call is made to an undefined method in a class, the magic method __call can intercept the call, so I could handle the situation as I see fit: http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods

Is there any mechanism provided in php whereby I can do the same thing with functions in global scope. The point is best illustrated with code:

    <?php
    function return_some_array(){
      $a = array();
      //Do stuff to array
      return array();
    }

    // Now i call the function like so:
    $give_me_array = return_some_array();

    // But sometimes I want the array to not contain zeroes, nulls etc.
    // so I call: 
    $give_me_array_filtered = return_some_array_filtered();

    // But i haven't defined return_some_array_filtered() anywhere.
    // Instead I would like to do something like so: 
    function __magic_call($function_name_passed_automatically){ 
      preg_match('/(.*)_filtered$/', $function_name_passed_automatically, $matches);
      $function_name_that_i_defined_earlier_called_return_some_array = $matches[1];
      if($matches){
        $result = call_user_func($function_name_that_i_defined_earlier_called_return_some_array);
        $filtered = array_filter($result);
        return $filtered;
      }
    }

    //So now, I could call return_some_other_array_filtered() and it would work provided I had defined return_some_other_array().
    //Or even Donkey_filtered() would work, provided I had defined Donkey() somewhere.
    ?>

Is this at all possible?

like image 516
Capstone Avatar asked Jul 21 '11 01:07

Capstone


People also ask

What are the magic functions that are available in the PHP?

The following method names are considered magical: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), and __debugInfo().

What is __ call () in PHP?

Method overloading ¶ __call() is triggered when invoking inaccessible methods in an object context. __callStatic() is triggered when invoking inaccessible methods in a static context. The $name argument is the name of the method being called.

What is __ method __ in PHP?

__FUNCTION__ and __METHOD__ as in PHP 5.0.4 is that. __FUNCTION__ returns only the name of the function. while as __METHOD__ returns the name of the class alongwith the name of the function.


1 Answers

Not as such.

If you had made a static method like return_some_array_filtered::go() then you could use PHP5's autoload() facility to dynamically create the class and method. After creation the call proceeds as usual. You may want to implement callStatic() on that class. Beware dynamically creating a class from scratch (without include()) in PHP is non-trivial.

like image 181
Magicianeer Avatar answered Nov 15 '22 00:11

Magicianeer