Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    Calling a function/constructing a class from an object's property

    Is it even possible to do this?
    So normally you can do:
    $var = 'substr';
    echo $var('My String', 1); // 'y String'

    But I can't seem to do:
    $var->method = 'substr';
    echo $var->method( 'My String', 1 );

    How can I make it work?

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

  • #3
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Well I do know of that but is there another way instead of assigning it to another variable?

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

  • #5
    Regular Coder byuhobbes85's Avatar
    Join Date
    Oct 2006
    Location
    Ames, Iowa, USA
    Posts
    116
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Take a look at the PHP __call() method. A Google search will give you a lot of results, but I found this one (http://www.garfieldtech.com/blog/php-magic-call) to be the most concise and relevant to your question.
    -- </byuhobbes>

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, call is an overload to invoke non-public or chained methods. Calling a function that is pointed to by a member needs call_user_func[_array], ReflectionFunction / ReflectionMethod (the latter inherits from former, so just ReflectionFunction is fine), or __invoke on the callback through a closure. Note the link you posted uses the call_user_func[_array] route.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The only reason I pointed the OP in the direction of using a temporary variable name is because from what I've experienced in the past (haven't tested this in a while though) call_user_func[_array] isn't very efficient.
    PHP Code:
    // Straight-up call, fast as !@#$%
    $text substr$text010 );

    // Not as fast but not at all slow
    $var->method 'substr';
    $func_name $var->method;
    $text $func_name$text010 );

    // Bit of a hog
    $var->method 'substr';
    $text call_user_func$var->method$text010 ); 
    Fou-Lu also brought up Reflection, which is a very reliable option though I have never taken the time to benchmark it versus the call_user_func[_array] solution.
    Last edited by kbluhm; 07-01-2010 at 04:42 PM.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Thats true yes, assumably variable functions are compile or link time evaluated.
    I benchmarked the reflections a few years ago, pretty sure I did the inokes as well. 5.0.x was slow if I recall, 5.2.x was fast, and I think it beat all. I did a closure with 5.3.0, and I recall Closure was ftw.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •