Liandri
Liandri

Reputation: 313

$this in Array of Closures in Class

Say there is a class for objects, let's use a User as an example. The User class contains it's own Rules to validate it's data before submitting. Before saving to the database, the Rules will be checked and any errors will be returned. Otherwise the update will run.

class User extends DBTable // contains $Rules, $Data, $Updates, and other stuff
{
    public __construct($ID)
    {
        parent::__construct($ID);
        // I'll only list a couple rules here...
        $this->Rules['Email'] = array(
            'Empty' => 'ValidateEmpty', // pre-written functions, somewhere else
            'Invalid' => 'ValidateBadEmail', // they return TRUE on error
            'Duplicate' => function($val) { return existInDatabase('user_table', 'ID_USER', '`Email`="'. $val .'" AND `ID_USER`!='. $this->ID);}
        );
        $this->Rules['Password'] = array(
            'Empty' => 'ValidateEmpty',
            'Short' => function($val) { return strlen($val) < 8; }
        );
        this->Rules['PasswordConfirm'] = array(
            'Empty' => 'ValidateEmpty',
            'Wrong' => function($val) { return $val != $this->Updates['Password']; }
        );
    }

    public function Save(&$Errors = NULL)
    {
        $Data = array_merge($this->Data, $this->Updates);
        foreach($this->Rules as $Fields => $Checks)
        {
            foreach($Checks as $Error => $Check)
            {
                if($Check($Data[$Field])) // TRUE means the data was bad
                {
                    $Errors[$Field] = $Error; // Say what error it was for this field
                    break; // don't check any others
                }
            }
        }
        if(!empty($Errors))
            return FALSE;

        /* Run the save... */

        return TRUE; // the save was successful
    }
}

Hopefully I posted enough here. So you'll notice that in the Duplicate error for Email, I want to check that their new email does not exist for any other user excluding themselves. Also PasswordConfirm tries to use $this->Updates['Password'] to make sure they entered the same thing twice.

When Save is run, it loops through the Rules and sets any Errors that are present.

Here is my problem:

Fatal error: Using $this when not in object context in /home/run/its/ze/germans/Class.User.php on line 19

This error appears for all closures where I want to use $this.

It seems like the combination of closures in an array and that array in a class is causing the problem. This Rule array thing works fine outside of a class (usually involving "use") and AFAIK closures are supposed to be able to use $this in classes.

So, solution? Work-around?

Thanks.

Upvotes: 0

Views: 798

Answers (1)

Jon
Jon

Reputation: 437584

The problem is with the Wrong validator. The validation method is called from here:

if($Check($Data[$Field])) // TRUE means the data was bad

This call is not made in an object context (the lambda is not a class method). Therefore $this inside the body of the lambda causes an error because it only exists when in an object context.

For PHP >= 5.4.0 you can solve the problem by causing the capture of $this:

function($val) { return $val != $this->Updates['Password']; }

In this case you will be able to access Updates no matter what its visibility is.

For PHP >= 5.3.0 you need to make a copy of the object reference and capturing that instead:

$self = $this;
function($val) use($self) { return $val != $self->Updates['Password']; }

In this case however, you will only be able to access Updates if it is public.

Upvotes: 2

Related Questions