PHP syntax for referencing self with late static binding
Posted
by Chris
on Stack Overflow
See other posts from Stack Overflow
or by Chris
Published on 2010-03-28T21:29:57Z
Indexed on
2010/03/28
21:33 UTC
Read the original article
Hit count: 359
php
|inheritance
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?
© Stack Overflow or respective owner