Jump to content

Building an attributes function without late static binding


dgruetter
 Share

Recommended Posts

Hi,

I have been trying to improve my PHP programming skills by following the Lynda.com Beyond the basics tutorial and they came to a section where they were building common database methods inside a database class.

 

Since they did not have PHP 5.3 (Where late static bindings is introduced) they had to place code referring to self:: inside of each class that needs this functionality. It seems that not having late static binding was binding self to the database class and not to other classes that call it statically. I will insert some code to explain this better.

 

Long story short, I was really interesting in getting an attributes function working and as I do not have late static bindg either, I cannot figure out how to call it from inside a class to make it function correctly.

 

Here is some code. Below is the attribute class they wrote. Each class declared an array of attributes for each of the classes that want to call this method like

 

protected static $db_fields = array('id', 'email', 'password', 'etc');

 

	
        protected function attributes() { 
        // return an array of attribute names and their values
  $attributes = array();
  foreach(self::$db_fields as $field) {
    if(property_exists($this, $field)) {
      $attributes[$field] = $this->$field;
    }
  }
  return $attributes;
}

 

They planned on moving this code to a DatabaseObject class after they get LSB but until then, have to include it in every class that wants use this.

 

Is there any way to write this as a function, include it in a class, and call the method without late static binding.. or even place this inside a database class and call it from another class without LSB?

 

Forgive me if I am not explaining this correctly, again, I am still learning.

 

My version of PHP is 5.2.6.

17392_.php

17393_.php

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.