473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Magic method __call() like function for global scope

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.overlo ading.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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function return_some_array(){
  3.   $a = array();
  4.   //Do stuff to array
  5.   return array();
  6. }
  7.  
  8. // Now i call the function like so:
  9. $give_me_array = return_some_array();
  10.  
  11. // But sometimes I want the array to not contain zeroes, nulls etc.
  12. // so I call: 
  13. $give_me_array_filtered = return_some_array_filtered();
  14.  
  15. // But i haven't defined return_some_array_filtered() anywhere.
  16. // Instead I would like to do something like so: 
  17. function __magic_call($function_name_passed_automatically){ 
  18.   preg_match('/(.*)_filtered$/', $function_name_passed_automatically, $matches);
  19.   $function_name_that_i_defined_earlier_called_return_some_array = $matches[1];
  20.   if($matches){
  21.     $result = call_user_func($function_name_that_i_defined_earlier_called_return_some_array);
  22.     $filtered = array_filter($result);
  23.     return $filtered;
  24.   }
  25. }
  26.  
  27. //So now, I could call return_some_other_array_filtered() and it would work provided I had defined return_some_other_array().
  28. //Or even Donkey_filtered() would work, provided I had defined Donkey() somewhere.
  29. ?>
  30.  
Is this at all possible?
Jul 21 '11 #1
1 2261
Dormilich
8,658 Expert Mod 8TB
Is there any mechanism provided in php whereby I can do the same thing with functions in global scope.
nope, there isn‘t.
Jul 21 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Yoyoma_2 | last post by:
Hello, i'me having a wierd problems with sessions. PHP 4.3.3, Register globals is on, and the sessions module is installed. if i have a page like this: <? session_start(); $_SESSION="blue";...
5
by: NotGiven | last post by:
I have an file I call using: require_once() In this file I have variables I'd like to use in the calling page and functions called by that page. How can I do this? example:
2
by: Wat | last post by:
If a class is not given a namespace, what does this imply? From inside of the class, if to call global functions, should global scope :: be used at all? If global scope :: used, what benefits...
1
by: Robert North | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've just started programming JS, and one question keeps nagging me: I can insert a <script> element anywhere in HTML, and when it's executed...
2
by: Bengt Richter | last post by:
Is this a well known bug that's been fixed? I couldn't find any discussion of it, but maybe my googling's off today ;-/ >>> def foo(): ... it = iter(range(10)) ... while True: ... ...
10
by: a.farhadi | last post by:
hi guys, I want to eval some javascript code at a function. and my code contains some variable and function definitions. my problem is that the code is evaluated in function scope not global...
5
by: george r smith | last post by:
In the MSDN documentation there is a reference to "the global scope". For example "You can declare types directly in the global scope." I have search extensively and can not find a definition of...
10
by: m.epper | last post by:
Hi to everybody. First of all sorry for my english, I'm italian. How can I execute a portion of code, in a function, into the global scope? Example: <?php
0
by: dba123 | last post by:
Why isn't my aspx page rendering before the process() method call? What is happening is my aspx page never renders, and the debug is jumping straight into my process() method when I run in...
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.