views:

64

answers:

2

When I learned PHP it was pretty much in procedural form, more recently I've been trying to adapt to the OOP way of doing things. Hoever the tutorials I've been following were produced before PHP 5.3 when late static binding was introduced.

What I want to know is how do I reference self when calling a function from a parent class.

For example these two methods were written for a User class which is a child of DatabaseObject. Right now they're sitting inside the User class, but, as they're used in other child classes of DatabaseObject I'd like to promote them to be included inside DatabaseObject.

public static function find_all()
  {
    global $database; 
    $result_set = self::find_by_sql("select * from ".self::$table_name);
    return $result_set;
  }

and:

protected function cleaned_attributes()
  {
    global $database; 
    $clean_attributes = array();
    foreach($this->attributes() as $key => $value) 
      {
        $clean_attributes[$key] = $database->escape_value($value);
      }
    return $clean_attributes;
  }

So I have three questions:

1) How do I change the self:: reference when I move it to the parent. Is it static:: or something similar?
2) When calling the function from my code do I call it in the same way, as a function of the child class eg User::find_all() or is there a change there also?
3) Is there anything else I need to know before I start chopping bits up?

A: 

1) How do I change the self:: reference when I move it to the parent. Is it static:: or something similar?

it's static

2) When calling the function from my code do I call it in the same way, as a function of the child class eg User::find_all() or is there a change there also?

it's User::find_all

3) Is there anything else I need to know before I start chopping bits up?

yes. Statics is not "the OOP way of doing things". OOP is about objects, not about classes. Static methods are in fact procedures with fancy syntax and are as bad as these.

stereofrog
I don't recall saying that statics were the OOP way of doing things, I was referring to the principles as a whole. Statics were one of the things that I had a question about, I was simply trying to add some background. Thanks for the answer though.
TooManyCooks
Also just to clarify, I drop parentheses for function calls? I thought including the () was standard for any function regardless of whether arguments were passed?
TooManyCooks
no prob buddy//
stereofrog
yes do include ()
stereofrog
A: 

Some other related bits to look up:

konforce
Thank you, get_called_class() seems self explanatory. Reflection will have me reading for a while yet though.
TooManyCooks