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 951085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:40:26+00:00 2026-05-15T23:40:26+00:00

This is what I have: All objects that can be persisted on the database

  • 0

This is what I have: All objects that can be persisted on the database extend the DatabaseObject abstract class, which has all the logic code to actually watch for attribute changes and run the databas queries.

I’m using two static variables to define object-specific details. I define them generically in the base class, and then supposedly I overwrite them in the actual database objects.

The problem is: When the code in the parent class is actually executed, it uses the old parent value instead of the current object value.

Here’s the code for the base class:

abstract class DatabaseObject {

    public $id;

    private static $databaseTable = NULL;
    private static $databaseFields = array();

    private $data = array();
    private $changedFields = array();

    public function IDatabaseObject($id) {
    $this->id = $id;
    $this->data = Database::GetSingle(self::$databaseTable, $id);

    Utils::copyToObject($this, $this->data, self::$databaseFields);
    }

    public static function Load($id) {
    return new self($userID);
    }

    public static function Create($data) {

    $id = Database::Insert(self::$databaseTable, $data);

    return new self($id);
    }

    public function Save() {
    $data = Utils::copyFromObject($this, $this->changedFields);

    Database::Update(self::$databaseTable, $data, $this->id);

    }

    public function __constructor() {
    // We do this to allow __get and __set to be called on public vars
    foreach(self::$databaseFields as $field) {
        unset($this->$field);
    }
    }

    public function __get($variableName) {
    return $this->$variableName;
    }

    public function __set($variableName, $variableValue) {
    // We only want to update what has been changed
    if(!in_array($variableName, $this->changedFields) && in_array($variableName, self::$databaseFields)) {
        array_push($this->changedFields, $variableName);
    }

    $this->$variableName = $variableValue;
    }
}

And here’s the code for one of the objects extending the base class above:

class Client extends DatabaseObject {

    public static $databaseTable = "clients";
    public static $databaseFields = array("name","contactName","primaryUserID","email","is_active","rg","cpf","cnpj","ie","addrType","addrName","addrNumber","addrComplement","addrPostalCode","addrNeighborhood","addrCity","addrState","addrCountry","phoneLandline","phoneFax","phoneMobile");

    public $name;
    public $contactName;

    public $primaryUserID;
    public $email;

    public $is_active;

    public $rg;
    public $cpf;
    public $cnpj;
    public $ie;

    public $addrType;
    public $addrName;
    public $addrNumber;
    public $addrComplement;
    public $addrPostalCode;
    public $addrNeighborhood;
    public $addrCity;
    public $addrState;
    public $addrCountry;

    public $phoneLandline;
    public $phoneFax;
    public $phoneMobile;

    public static function Load($id) {
 return new Client($id);
    }

}

What am I doing wrong here? Is there another way I can achieve the same result?

A brief addendum: I declare the attributes in the class body mainly to let it be seen by the NetBeans’ auto-complete feature.

  • 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-05-15T23:40:27+00:00Added an answer on May 15, 2026 at 11:40 pm

    You are looking for Late Static Binding.

    So you need to use:

    static::$databaseTable
    

    instead of

    self::$databaseTable
    

    This feature is available as of PHP 5.3. Simulating this in PHP 5.2 is very hard, because of two reasons: get_called_class is available only since PHP 5.3, too. Therefore it must be simulated, too, using debug_backtrace. The second problem is, that if you have the called class, you still may not use $calledClass::$property because this is a PHP 5.3 feature, too. Here you need to use eval or Reflection. So I do hope that you have PHP 5.3 😉

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

Sidebar

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.