Jump to content

oop - parent and child question


ricky spires

Recommended Posts

Hello.

 

i have some code that works with out using parent and child but i dont think im doing it in the correct oop way. would someone maybe please just have a little look for me please.

 

by the way.. i know im not ment to use global but thats how i was shown to do that on my tutorial videos.. i'll address that in another post :)

 

so.

in this example i have this pages class

<?PHP
require_once(LIB_PATH.DS.'database.php');

class Pages {



protected static $table_name="pages";

protected static $db_fields = array(
'id',
'pageName',
'contTemp_id',
'contElements_id',
	'title',
'sub_title',
'description',
'about',
'image',
'created',
'dateMod',
'timeMod');


public $id;
public $pageName;
public $contTemp_id;
public $contElements_id;
public $title;
public $sub_title;
public $description;
public $about;
public $image;
public $created;
public $dateMod;
public $timeMod;



// "new" is a reserved word so we use "make"(or "build")
public static function make(

$id="",
$pageName="",
$contTemp_id="",
$contElements_id="",
	$title="",
$sub_title="",
$description="",
$about="",
$image="",
$created="",
$dateMod="",
$timeMod="") {

	if(!empty($title)) {

		$kw = new NavL3();
		$kw->id = (int)$id;
		$kw->pageName = $pageName;
		$kw->contTemp_id = (int)$contTemp_id;
		$kw->contElements_id = (int)$contElements_id;
		$kw->title = $title;
		$kw->sub_title = $sub_title;
		$kw->description = $description;
		$kw->about = $about;
		$kw->image = $image;
		$kw->created = $created;
		$kw->dateMod = $dateMod;
		$kw->timeMod = $timeMod;

		return $kw;
	}else{
		return false;
	}
} //end function make


public static function find_by_pageName($id){
    global $database;

	$sql = "SELECT * FROM ".self::$table_name." WHERE id='".$database->escape_value($id)."'";
	$result_array = self::find_by_sql($sql);

	return !empty($result_array) ? array_shift($result_array) : false;
}


public static function find_by_pageID($pageID=0){
	global $database;
	$sql = "SELECT * FROM ".self::$table_name." WHERE id=".$database->escape_value($pageID)." LIMIT 1";
	$result_array = self::find_by_sql($sql);
	return !empty($result_array) ? array_shift($result_array) : false;
}


// Common Database Methods
public static function find_all(){
	global $database;
	$sql = "SELECT * FROM ".self::$table_name."";
	return self::find_by_sql($sql);
}

public static function find_by_id($id=0){
	global $database;
	$sql = "SELECT * FROM ".self::$table_name." WHERE id=".$database->escape_value($id)." LIMIT 1";
	$result_array = self::find_by_sql($sql);
	return !empty($result_array) ? array_shift($result_array) : false;
}

public static function find_by_sql($sql){
	global $database;
	$results_set = $database->query($sql);
	$object_array = array();
	while($row = $database->fetch_array($results_set)){
		$object_array[] = self::instantiate($row);
	}
	return $object_array;
}

public static function count_all(){
	global $database;
	$sql = "SELECT COUNT(*) FROM ".self::$table_name;
	$results_set = $database->query($sql);
	$row = $database->fetch_array($results_set);
	return array_shift($row);
}

public static function instantiate($result){
	$object = new self;
	foreach($result as $attribute => $value){
		if($object->has_attribute($attribute)){
			$object->$attribute = $value;
		}
	}
	return $object;
}

private function has_attribute($attribute){
	$object_vars = $this->attributes();
	return array_key_exists($attribute, $object_vars);
}


protected function attributes(){
	$attributes = array();
	foreach(self::$db_fields as $field){
		if(property_exists($this, $field)){
			$attributes[$field] = $this->$field;
		}
	}
	return $attributes;
}

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

public function save(){
	return !empty($this->id) ? $this->update() : $this->create();
}

public function create(){
	global $database;
	$attributes = $this->sanitized_attributes();
	$sql = "INSERT INTO ".self::$table_name." (";
	$sql .= join(", ", array_keys($attributes));
	$sql .= ") VALUES ('";
	$sql .= join("', '", array_values($attributes));
	$sql .= "')";
	if($database->query($sql)){
		$this->id = $database->insert_id();
		return true;
	}else{
		return false;
	}
}

public function update(){
	global $database;
	$attributes = $this->sanitized_attributes();
	$attribute_pairs = array();
	foreach($attributes as $key => $value){
		$attribute_pairs[] = "{$key}='{$value}'";	
	}
	$sql = "UPDATE ".self::$table_name." SET ";
	$sql .= join(", ", $attribute_pairs);
	$sql .= " WHERE id=".$database->escape_value($this->id);
	$database->query($sql);
	return ($database->affected_rows() == 1) ? true : false;
}

public function delete(){
	global $database;
	$sql = "DELETE FROM ".self::$table_name." ";	
	$sql .= "WHERE id=".$database->escape_value($this->id);
	$sql .= " LIMIT 1";	
	$database->query($sql);
	return ($database->affected_rows() == 1) ? true : false;
}



} //end class blogs
?>

 

 

 

and i have this conttemp class

<?PHP
require_once(LIB_PATH.DS.'database.php');

class Conttemps {

protected static $table_name="contTemps";

protected static $db_fields = array(
'id',
'name',
'created',
'dateMod',
'timeMod');


public $id;
public $name;
public $created;
public $dateMod;
public $timeMod;



// "new" is a reserved word so we use "make"(or "build")
public static function make(

$id,
$name="",
$created="",
$dateMod="",
$timeMod="") {

	if(!empty($name)) {

		$kw = new Templates();
		$kw->id = (int)$id;
		$kw->name = $name;
		$kw->created = $created;
		$kw->dateMod = $dateMod;
		$kw->timeMod = $timeMod;

		return $kw;
	}else{
		return false;
	}
} //end function make



public function find_contTemp($pageID){

$page = Pages::find_by_pageID($pageID);
$pageCID = $page->contTemp_id;

$sql = "SELECT * FROM ".self::$table_name." WHERE id=".$pageCID."";
$result_array = self::find_by_sql($sql);
	return !empty($result_array) ? array_shift($result_array) : false;
}


// Common Database Methods
public static function find_all(){
	global $database;
	$sql = "SELECT * FROM ".self::$table_name."";
	return self::find_by_sql($sql);
}

public static function find_by_id($id=0){
	global $database;
	$sql = "SELECT * FROM ".self::$table_name." WHERE id=".$database->escape_value($id)." LIMIT 1";
	$result_array = self::find_by_sql($sql);
	return !empty($result_array) ? array_shift($result_array) : false;
}

public static function find_by_sql($sql){
	global $database;
	$results_set = $database->query($sql);
	$object_array = array();
	while($row = $database->fetch_array($results_set)){
		$object_array[] = self::instantiate($row);
	}
	return $object_array;
}

public static function count_all(){
	global $database;
	$sql = "SELECT COUNT(*) FROM ".self::$table_name;
	$results_set = $database->query($sql);
	$row = $database->fetch_array($results_set);
	return array_shift($row);
}

public static function instantiate($result){
	$object = new self;
	foreach($result as $attribute => $value){
		if($object->has_attribute($attribute)){
			$object->$attribute = $value;
		}
	}
	return $object;
}

private function has_attribute($attribute){
	$object_vars = $this->attributes();
	return array_key_exists($attribute, $object_vars);
}


protected function attributes(){
	$attributes = array();
	foreach(self::$db_fields as $field){
		if(property_exists($this, $field)){
			$attributes[$field] = $this->$field;
		}
	}
	return $attributes;
}

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

public function save(){
	return !empty($this->id) ? $this->update() : $this->create();
}

public function create(){
	global $database;
	$attributes = $this->sanitized_attributes();
	$sql = "INSERT INTO ".self::$table_name." (";
	$sql .= join(", ", array_keys($attributes));
	$sql .= ") VALUES ('";
	$sql .= join("', '", array_values($attributes));
	$sql .= "')";
	if($database->query($sql)){
		$this->id = $database->insert_id();
		return true;
	}else{
		return false;
	}
}

public function update(){
	global $database;
	$attributes = $this->sanitized_attributes();
	$attribute_pairs = array();
	foreach($attributes as $key => $value){
		$attribute_pairs[] = "{$key}='{$value}'";	
	}
	$sql = "UPDATE ".self::$table_name." SET ";
	$sql .= join(", ", $attribute_pairs);
	$sql .= " WHERE id=".$database->escape_value($this->id);
	$database->query($sql);
	return ($database->affected_rows() == 1) ? true : false;
}

public function delete(){
	global $database;
	$sql = "DELETE FROM ".self::$table_name." ";	
	$sql .= "WHERE id=".$database->escape_value($this->id);
	$sql .= " LIMIT 1";	
	$database->query($sql);
	return ($database->affected_rows() == 1) ? true : false;
}




} //end class blogs
?>

 

 

the bit im interested in is this function in the Conttemp class

 

public function find_contTemp($pageID){

$page = Pages::find_by_pageID($pageID);
$pageCID = $page->contTemp_id;

$sql = "SELECT * FROM ".self::$table_name." WHERE id=".$pageCID."";
$result_array = self::find_by_sql($sql);
	return !empty($result_array) ? array_shift($result_array) : false;
}

 

it call a function in from the Pages class.

 

i could take the function from here and put it in the Pages class i guess but i was wondering about Exends.

 

should i do class Conttemps extends Pages {

 

and use something like

 

public function find_contTemp($pageID){

$pageCID= Pages::contTemp_id;


$sql = "SELECT * FROM ".self::$table_name." WHERE id=".$pageCID."";
$result_array = self::find_by_sql($sql);
	return !empty($result_array) ? array_shift($result_array) : false;
}

 

i guess that wont work because its not getting the $pageID

 

but anyway..

 

any thoughts ????

 

thanks

rick

Link to comment
Share on other sites

IMO, you really need to stop and take your time to grasp the basics of OOP.  There are a lot of problems with what you have, and, honestly, you'd be better served trying to start over from scratch than hammer away at what you currently have.

 

Start here: http://php.net/manual/en/language.oop5.php

 

Then go here (a MUST HAVE step if you want to start thinking like an OOP in PHP programmer): http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/ref=sr_1_1?ie=UTF8&qid=1318537549&sr=8-1

 

Finally, go here: http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_2?s=books&ie=UTF8&qid=1318537603&sr=1-2

 

Those three sources are definitely not all inclusive re: OOP, but they will teach you the correct way to approach common programming problems and to 'see' things in an OOP manner.  It's certainly a more efficient way of doing it than trying to learn fundamental design methodology entirely from an online forum.  Besides, a lot of what we give as advice stems from those resources (and others) in addition to our collective experience.  You might as well have access to those resources yourself if you're serious about pursuing OOP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.