0

Is it possible to define a function that get's called when a object isn't defined? So I can do $makeUpOntheFlyObjectNames->something(); in the function get the object name, and the function name and a array of it's arguments, or a empty array if it has none. For what it was trying to call? Had an idea to make stuff like $varName->length(); work in V5.x for strings and stuff. Was trying to Google this but was getting stuff like class_exists and stuff back, so figured a human would have a better answer. But I don't think it's possible, just wanted to check here to be sure.

So if the object wasn't found, and was calling length, check if it's a string and then return the result of strlen, else if it's not a string emulate php's own error.. Just some idea I had, but not sure if the first part of detection is even possible.

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Keverw
  • 3,736
  • 7
  • 31
  • 54
  • You could work some autoload magic. – John V. Jan 27 '13 at 08:04
  • I'm afraid this isn't currently possible unless you have the ability to modify the classes you want this behaviour in (using the `__call` magic method). There isn't anything that can step in globally *(even the error handling functions don't trigger for missing methods)* -- http://stackoverflow.com/questions/3456763/magic-functions-call-for-functions -- http://stackoverflow.com/questions/2865865/are-there-equivalents-to-rubys-method-missing-in-other-languages – Pebbl Jan 27 '13 at 09:26

1 Answers1

0

You can try the following:

$objName = 'test';
//check if the object exists
if(isset($$objName)){
//object exists...
}
else{
//do what you want here
}

try to run it "as is" or add a line before this code, for example:

$test = array(1=>'a',2=>'b');
$objName = 'test';
...

and see how you "fall" into the "if" instead of the "else"

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129