开发者

PHP syntax for referencing self with late static binding

开发者 https://www.devze.com 2022-12-24 05:17 出处:网络
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.

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 be开发者_StackOverflowfore I start chopping bits up?


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.


Some other related bits to look up:

  • get_called_class()
  • Reflection
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号