We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Am I dealing with late static bindings correctly in php??

FairyBusiness
on
Medium Priority
290 Views
Last Modified: 2012-05-11
Hi,

I have a class DatabaseObjects that is a child of a class Users.  I decided to take some of my common functions from Users and put them into a class that can be used for other things as well.

I thought I should replace:

self::$table   with   static::$table

Is that not the way to do it?        http://auroriella.com/index.php
<?php
require_once 'includes/database.php';

class DatabaseObject { 

// Common Databaes Methods

protected static $table = "users";
private $object;

public static function find_all() {
	return static::find_by_sql("SELECT * FROM " . static::$table);
}
public static function find_by_id($id=0) {
	global $database;
	$result = static::find_by_sql("SELECT * FROM  " . static::$table . " WHERE id={$id} LIMIT 1");
	return !empty($result) ? array_shift($result) : false; //array_shift pulls the first item out
}
public static function find_by_sql($sql="") {
	global $database;
	$result = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result)) {
		$object_array[] = static::instantiate($row);
	}
	return $object_array; // Must return an array!
}
private static function instantiate($record) {
	$class_name = get_called_class();
	$object = new $class_name; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
		}
	}
	return $object;
}
private function has_attribute($attribute) {
	// get_object_vars returns an associative array with all attributes
	// (including private ones) as the keys and their current value as the value
	$object_vars = get_object_vars($this);
	// We don't care about the value, we just want to know if the key exists
	// Will return true or false
	return array_key_exists($attribute, $object_vars);
}

} // End class User
?>

Open in new window

Comment
Watch Question

Author

Commented:
Ok, instead of using static every time I used parent instead. Now did I just cause myself more problems down the road or will parent do the same thing as static??
<?php
require_once 'includes/database.php';

class DatabaseObject { 

// Common Databaes Methods

protected static $table = "users";
private $object;

public static function find_all() {
	return parent::find_by_sql("SELECT * FROM " . parent::$table);
}
public static function find_by_id($id=0) {
	global $database;
	$result = parent::find_by_sql("SELECT * FROM  " . parent::$table . " WHERE id={$id} LIMIT 1");
	return !empty($result) ? array_shift($result) : false; //array_shift pulls the first item out
}
public static function find_by_sql($sql="") {
	global $database;
	$result = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result)) {
		$object_array[] = parent::instantiate($row);
	}
	return $object_array; // Must return an array!
}
private static function instantiate($record) {
	$class_name = get_called_class();
	$object = new $class_name; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
		}
	}
	return $object;
}
private function has_attribute($attribute) {
	// get_object_vars returns an associative array with all attributes
	// (including private ones) as the keys and their current value as the value
	$object_vars = get_object_vars($this);
	// We don't care about the value, we just want to know if the key exists
	// Will return true or false
	return array_key_exists($attribute, $object_vars);
}

} // End class User
?>

Open in new window

CERTIFIED EXPERT
Top Expert 2004
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.