Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:11:05+00:00 2026-06-18T07:11:05+00:00

Say there is a class for objects, let’s use a User as an example.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:11:06+00:00Added an answer on June 18, 2026 at 7:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We can use reflection to create the objects. Let's say I have a class
Let's say I have two Class objects. Is there a way to check whether
Say, there is a following example: class Super { public int i = 3;
In VB.NET there is a keyword 'shadows'. Let's say I have a base class
Let's say you have a class called Customer, which contains the following fields: UserName
Let say if there is already class properties of @property (strong, nonatomic) JJNode *leftChild;
Let's say I have objects which look very roughly like this: class object {
I have array of Objects (let say that class name is Snap) Snap. Is
Let's say I have a model as follows. models.py class Profile(models.Model): user = models.OneToOneField(User)
Let's say I've got an array of objects from class Users. Class Users contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.