Advanced Minds

  • Decrease font size
  • Default font size
  • Increase font size
  • default color
  • red color
  • green color
FireBoard
Welcome, Guest
Please Login or Register.    Lost Password?
objects in php new $foo->className(); Class name must be a valid object or a string (1 viewing) (1) Guests
Go to bottom Post Reply Favoured: 0
TOPIC: objects in php new $foo->className(); Class name must be a valid object or a string
#2033
Jack Bates (Visitor)
Click here to see the profile of this user
Birthdate:
objects in php new $foo->className(); Class name must be a valid object or a string  
I am trying to load PHP _object_s stored in a data_base_, where the class name is stored in a column: $_object_ = new $resultSet-getString(1); This fails for the same reason that the following fails: <?php class Foo {   public function className()   {     return 'Foo';   } } $foo = new Foo; $bar = new $foo-className(); Fatal error: Class name must be a valid _object_ or a string in test.php on line 12 I guess this error is due to the confusion of parsing () as the argument list for the className function, or the Foo constructor... I work around this error by using a temp variable: $tmp = $foo-className(); $bar = new $tmp; - however the above reads like hacky code : ( When calling dynamically named functions, I generally use call_user_func() to avoid awkwardness with $_object_-$tmp($arg1, ...) In other words, I prefer: call_user_func(array($_object_, 'get'.$someName), $arg1, ...); - to: $tmp = 'get'.$someName; $_object_-$tmp($arg1, ...); However there does not appear to be an analog of call_user_func() for constructing new instances of dynamically named classes? If I recall correctly, there was also a way to work around calling dynamically named functions (e.g. $_object_-$tmp($arg1, ...) using curly braces: $_object_-{'get'.$someName}($arg1, ...); - however I cannot recall the exact syntax. Can anyone confirm that there is a curly brace syntax for calling dynamically named functions? Could it be applied to instantiating dynamically named classes? Can anyone recommend a cleaner alternative to: $tmp = $foo-className(); $bar = new $tmp; Thanks and best wishes, Jack
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#2034
Stut (Visitor)
Click here to see the profile of this user
Birthdate:
objects in php new $foo->className(); Class name must be a valid object or a string  
name is stored in a column: $_object_ = new $resultSet-getString(1); This fails for the same reason that the following fails: <?php class Foo {  public function className()  {    return 'Foo';  } } $foo = new Foo; $bar = new $foo-className(); I would rather have a factory method that returns a new instance of   the class. There's no need for the outside world to know the class name. <?php    class Foo    {      public function newInstance()      {        return new self();      }      public function test($a)      {        echo 'test: '.$a. n ;      }    }    $foo = new Foo;    $foo-test('foo');    $bar = $foo-newInstance();    $bar-test('bar'); ? However, if you insist on doing it your way can I make a small   suggestion? It's better to spend your time on functionality rather   than finding ways to save some typing. I see no reason to try to   combine the two statements - saving typing and a pitiful amount of   disk space are the only benefits. -Stut
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#2035
Casey (Visitor)
Click here to see the profile of this user
Birthdate:
objects in php new $foo->className(); Class name must be a valid object or a string  
name is stored in a column: $_object_ = new $resultSet-getString(1); This fails for the same reason that the following fails: <?php class Foo {  public function className()  {    return 'Foo';  } } $foo = new Foo; $bar = new $foo-className(); Fatal error: Class name must be a valid _object_ or a string in test.php on line 12 I guess this error is due to the confusion of parsing () as the argument list for the className function, or the Foo   constructor... I work around this error by using a temp variable: $tmp = $foo-className(); $bar = new $tmp; - however the above reads like hacky code : ( When calling dynamically named functions, I generally use call_user_func() to avoid awkwardness with $_object_-$tmp($arg1, ...) In other words, I prefer: call_user_func(array($_object_, 'get'.$someName), $arg1, ...); - to: $tmp = 'get'.$someName; $_object_-$tmp($arg1, ...); However there does not appear to be an analog of call_user_func() for constructing new instances of dynamically named classes? If I recall correctly, there was also a way to work around calling dynamically named functions (e.g. $_object_-$tmp($arg1, ...) using curly braces: $_object_-{'get'.$someName}($arg1, ...); - however I cannot recall the exact syntax. Can anyone confirm that there is a curly brace syntax for calling dynamically named functions? Could it be applied to instantiating dynamically named classes? Can anyone recommend a cleaner alternative to: $tmp = $foo-className(); $bar = new $tmp; Thanks and best wishes, Jack Does... <?php   $bar = new $foo-className()(); ? ...work? Otherwise, I'd just do... <?php   $className = $foo-className();   $bar = new $className; ? ...instead of $tmp. - Hide quoted text -- Show quoted text -
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop
905 905 authorization failed nieautoryzowano sprawdz autoryzacje
mercedes diving centre news channel couponmountain finance news