5

For example, I have such a code:

<?php
$A = array(
    'echoSmth' => function(){ 
        echo 'Smth';
    }
);

$A['echoSmth']();  // 'Smth'
?>

It works fine! But If $A is not just a variable, but a Class method - than this doesn't work:

<?php

class AA {

    public $A = array(
        'echoSmth' => function(){ // Parse Error here!
            echo 'Smth';
        }
    );

}

// Fixed call:
$t = new AA();
$t->A['echoSmth']();
// no matter what call, because error occurs early - in describing of class

?>

Why doesn't it work? It displays: Parse error: syntax error, unexpected T_FUNCTION

P.S. Sorry, I've made some mistakes in the way I call the method, I was in hurry. But there's no matter how I call. Error ocurrs even if I just declare class, without calling

4
  • 2
    try $t->a['echoSmth'](); without the dollar sign Commented Apr 30, 2011 at 15:04
  • 3
    @usoban: a in uppercase. Commented Apr 30, 2011 at 15:09
  • @marcel right, I was looking at the last line, where a is lowercase. my bad :) Commented Apr 30, 2011 at 15:11
  • Yes, I had mistakes in call of function. But I forgot to say, that error occurs when describing class. Without calling it occurs too Commented Apr 30, 2011 at 15:24

3 Answers 3

3

As far as I'm aware, you can't have anything dynamic when defining a class member, you can however set it dynamically as below. So basically, you can't do it for the same reason that you can't do this: public $A = functionname();

Also, your call signature was incorrect, I've fixed it in my example.

<?php
class AA {
    public $A = array();

    public function __construct() {
        $a = function() {
            echo 'Smth';
        };
        $this->A['echoSmth'] = $a;
    }
}

$t = new AA();
$t->A['echoSmth']();

Alternatively, you could define the whole array within __construct(), containing the method (so basically shift your code).

Sign up to request clarification or add additional context in comments.

Comments

2

I got this to work. Not sure why direct declaration is not permitted though.

class AA {
    var $A;
    public function __construct() {
        $this->A = array(
            'echoSmth' => function(){
                echo 'Smth';
            }
        );
    }
}

$t = new AA();
$t->A['echoSmth']();

EDIT: I also saw and corrected $t->$a first, but I needed to move the declaration as well to make it work.

9 Comments

He has an error, he's using $t->$a instead of $t->a. You got the code right, the OP didn't. There's no php issue here, just a simple mistake when accessing the member.
@Michael Incorrect, there's a parse error for defining a closure when defining the value of $A (But yes that was an error).
That one is also true rudi, I skimmed trough the class and assumed he did make sure he hasn't got parse errors - apparently, you can't trust anyone these days, thanks for pointing that out :)
@Michael Yes when I first looked it over I was thinking the same thing, but the reasoning is as defined in my answer! :-)
@Michael J.V. Just that change (or even removing the last row) still gives the error: Parse error: syntax error, unexpected T_FUNCTION.
|
1

well , it kinda works ...

<?php

    class Bleh
    {

        public $f = array();

        public function register( $name , $func )
        {

            $this->f[ $name ] =  $func;

        }

    }   


    $foo = new Bleh;
    $foo->register( 'bar' , function(){ echo 'foobar'; } );

    $foo->f['bar']();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.