Jump to content

method creation on generic object


alber

Recommended Posts

Hello.

 

I can define properties (data members) but I cant define a method for an object belongs to stdClass(generic) class.

 

for example :

 

$myObject = new stdClass();

 

$myObject->prop1 = "hello"; // works OK

 

function fun1()

{

echo "bye";

}

$myObject->method1 = fun1; // object accept the asignament

 

$myObject->metodo1();

 

the last sentence show an error :

server message :

 

Fatal error: Call to undefined method stdClass::metodo1() in /home/.../public_html/pru/objetos.php on line 63

 

 

Thanks for any help.

 

Alber

Link to comment
Share on other sites

$myObject->method1 = fun1; // object accept the asignament

 

No it doesn't. It just sets $myObject->method1 to "fun" (string) or assigns it a value of 'fun' constant if such constant is defined.

 

As far as I know you cannot add methods dynamically to an object.

Link to comment
Share on other sites

Are you sure that you've not made a mistake with your spelling:

 

$myObject->method1 = fun1; // object accept the asignament

 

$myObject->metodo1();

 

sorry. I want to translate to english and forgot change the "metodo1 to method1.

But the question in the same.

 

$myObject->method1(); //it doesn work

Link to comment
Share on other sites

$myObject->method1 = fun1; // object accept the asignament

 

No it doesn't. It just sets $myObject->method1 to "fun" (string) or assigns it a value of 'fun' constant if such constant is defined.

 

As far as I know you cannot add methods dynamically to an object.

 

bad news.

It is useful to define object without a custom class like in javascript.

 

thanks.

Link to comment
Share on other sites

As far as I know you cannot add methods dynamically to an object.

 

class Foo {}

$foo = new Foo();
$foo->bar = function() {
echo 'test';
};

 

I wouldn't recommend that kind of code though.

 

It is useful to define object without a custom class like in javascript.

 

Javascript uses prototype based object orientation while PHP uses class based object orientation.

Link to comment
Share on other sites

For the stdObject, I'm not sure. For your own defined class, you can use the magic __call method, and create a static registerMethod method to dynamically add functions. consider the following

class Dynamic {
//This is an array with all the classes methods. we can define the methods outside the class
  static protected $methods = array();

//this will register any method we want to the class, by adding it to the array
  public static function registerMethod($method) {
    self::$methods[] = $method;
  }

//this is where the "magic" happens. This function gets called when you call the method
//it will see if the function is in the methods array, and then call it
  private function __call($method, $args) {
    if (in_array($method, self::$methods)) {
      return call_user_func_array($method, $args);
    }
  }
}

//example

//define a simple function
function test() {
  print "Hello World";
}
//call our static method to add the test function to the class
Dynamic::registerMethod('test');
//instantiate the class
$d = new Dynamic();
//now call the method
$d->test();

 

if you wish to read more about it,  Here is where I got the idea from, and you can also view the rest of PHPs magic methods at the manual

 

As far as I know you cannot add methods dynamically to an object.

 

class Foo {}

$foo = new Foo();
$foo->bar = function() {
echo 'test';
};

 

I wouldn't recommend that kind of code though.

 

I actually tried something similar to this at first, and it didn't work. I copy pasted this also, and it doesn't work. error:

Parse error: syntax error, unexpected T_FUNCTION in C:\Server\htdocs\text.php on line 8

 

 

Link to comment
Share on other sites

class Foo {}

$foo = new Foo();
$foo->bar = function() {
echo 'test';
};

 

Just to clarify (it might not be clear for the OP), Daniel's code still does not define the 'bar' method, it assigns an anonymous function to the bar property (which might be fine).

 

Link to comment
Share on other sites

I actually tried something similar to this at first, and it didn't work. I copy pasted this also, and it doesn't work. error:

Parse error: syntax error, unexpected T_FUNCTION in C:\Server\htdocs\text.php on line 8

 

Anonymous functions are available in PHP5.3+

Link to comment
Share on other sites

I actually tried something similar to this at first, and it didn't work. I copy pasted this also, and it doesn't work. error:

Parse error: syntax error, unexpected T_FUNCTION in C:\Server\htdocs\text.php on line 8

 

Anonymous functions are available in PHP5.3+

 

ahh ok. I always wondered when they would add those in.

Link to comment
Share on other sites

Also note that you won't be able to call $foo->bar() — you need to use call_user_func($foo->bar) or $foo->bar->__invoke()

 

Yeah, but to be honest, I consider that a bug more than a feature. When $bar('something') is callable, then $foo->bar('something') should be as well (assuming both refer to anonymous functions).

Link to comment
Share on other sites

Yeah, but to be honest, I consider that a bug more than a feature. When $bar('something') is callable, then $foo->bar('something') should be as well (assuming both refer to anonymous functions).

 

It would be much more intuitive to be able to do that, I agree.

 

It also seems that method created this way can't use $this or self::

I'm toying with it right now, so I might be wrong :P

 

There is no concept of $this or self:: in these anonymous functions.  You could pass the object ($foo) through to the function (in place of $this) using use and have Foo:: (in place of self::) but it's not a pretty solution.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.