Kensing
Kensing

Reputation: 107

PHP closure: Extending scope of variable to function

First: I have read all of the possible duplicate posts for this, and I have looked over several sources of documentation and examples, which I have replicated in the below code. However I'm getting syntax errors when writing this in Aptana 3. Is this syntax not legal, or is it perhaps a problem with my environment?

class Story {       
    private $storyText;

    function build () use ($storyText)  {
        $storyText .= "blabla";
    };
}

Upvotes: 0

Views: 58

Answers (1)

hek2mgl
hek2mgl

Reputation: 157957

It is a syntax error. The use statement in this form isn't allowed for a class method. It is for closures only.

I guess you want something like this:

class Story {       
    private $storyText;

    public function build ()   {
        $this->storyText .= "blabla";
    };
}

Try to start with PHP's OOP Basics described in the manual.

Upvotes: 2

Related Questions