Dynamic variable scope in extended classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JustinMacCreery
Forum Newbie
Posts: 1
Joined: Wed Aug 21, 2013 7:20 am

Dynamic variable scope in extended classes

Post by JustinMacCreery »

Hello, I have a base class where I have my set and get functions, as such:

Code: Select all

class Utilities{
		
		function __construct(){
			global $db;
			$this->db =& $db;
		}
		
		// GET function for all properties
		function get($property){
			global $$property;
			return $this->$property;
		}
		
		// SET function for all properties
		function set($property, $value){
			global $$property;
			if (is_object($$property)){
				// incomplete
			}else{
				$this->$property = $value;
			}
		}
                 }
Then, that class is the base of all my other classes. The db class is mysqli_extends. My question is, when I extend the class, like this:

Code: Select all


class Thing extends Utilities{
	
	// GET id in constuct and build thing
	// (if this is a new object, be sure to pass 0 to $thingId)

	public function __construct($thingId, $newArray = array()){
		parent::__construct();
		if($thingId > 0){
			$this->set('thing', $thingId);
			$query = "SELECT * FROM thing WHERE id = '".$thingId."'";
			$dataArray = $db->query($query)->fetch_assoc();
			
			// CREATE variables from data
			foreach($dataArray as $k=>$v){
				$this->set($k, $v);
			}
			return $thingId;
		}else{
			// we're going to make a new one. 
			// CREATE the data table

			$this->db->insert('thing', $newArray);
			foreach($newArray as $k=>$v){
				//parent::set($k, $v);
				$this->set($k, $v);
			}
			// RETURN the new id
			$this->set('thing', $this->db->insert_id);
			return $this->db->insert_id;
		}
	}
	
	public function save(){
		// SAVE all portions that are part of Thing
		$dataArray = array('additionalType' => $this->get('additionalType'), 'description' => $this->get('description'), 'image' => $this->get('image'), 'name' => $this->get('name'));
		$this->db->update('thing', $dataArray, 'id = "'.$this->get('thing').'"');
		return $this->get('thing');
	}
}
as Thing gets extended, new variables are added based on database field names, which in turn are used to title my microdata tags (The basis for these classes is to parallel what I'm seeing at schema.org). Is there a better way than to use global variables? Will this cause issues if I use extend my Thing class with, say Article. Then, on in my Page class, which also extends Thing, I use my Article class. Are the global variables going to get over written?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Dynamic variable scope in extended classes

Post by AbraCadaver »

You are already setting the object properties in the class, why in the world are you using globals? Get rid of those.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Dynamic variable scope in extended classes

Post by Christopher »

UGH! That code is the most horrible thing I have seen in a long time! That Utilities class is pure evil. Why are you using globals instead of passing values as parameters? Why does your constructor do SELECTs and INSERTs?!? I won't even ask why the constructor has return values!?!

Honestly, pass the $db object to the constructor and assign it to a $this->db property so you have a functional object when instantiated. The create find() and insert() functions like the standard data access patterns have. And pass the array of field values, primary key and any other values to the find(), insert() and update() methods. A save() method would probably be assumed to internally decide whether to call the insert() or update() methods based on the presence of a primary key.

Make the world a better place and stop writing code like this! :drunk:
(#10850)
Post Reply