verycleanteeth Posted August 19, 2008 Share Posted August 19, 2008 Hello! I'm a bit new to OOP and am trying to find a way to add functions to already existing objects. I'm pulling tables from a mysql "character" database with mysql_fetch_object, and these objects are organized just how I need them, except they lack the functions normally defined in the "character" class. Thanks! Link to comment https://forums.phpfreaks.com/topic/120298-adding-functions-to-existing-objects/ Share on other sites More sharing options...
knowj Posted August 19, 2008 Share Posted August 19, 2008 Create a child class and initiate the object off the child class rather than the parent for example: <?php //Database class to implement singleton pattern on top of mysqli class database extends mysqli { //create a singleton instance private $report; private static $instance = NULL; //private constructor to prevent direct access private function __construct() { parent::__construct(constants::DBHOST, constants::DBUSER, constants::DBPASS, constants::DBNAME); if(mysqli_connect_errno()) { throw new Exception(mysqli_connect_error(), mysqli_connect_errno()); } parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 10); } //public create instance function to create singleton public function getinstance() { if(self::$instance === null) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } //prevent SQL injection function antisql($data) { if (is_array($data)) { foreach ($data as $name=>$value) { if (is_array($data[$name])) { foreach ($data[$name] as $name2=>$value) { $data[$name][$name2] = $this->real_escape_string($value); } } else { $data[$name] = $this->real_escape_string($value); } } } else { $data = $this->real_escape_string($data); } return $data; } //prevent clone public function __clone() { throw new Exception("Cannot clone ".__CLASS__." class"); } public function __destruct() { parent::close(); } } ?> within your php file you could do something along the lines of <?php require_once 'class_database.php'; $db = database::getinstance(); $status = $db->antisql($_GET['status']); $result = $db->query("SELECT chair_type FROM `mytable` WHERE `chair`='$status'"); while ($row = $result->fetch_array(MYSQLI_ASSOC) { echo $row['chair_type']; } ?> Hope this helps. Link to comment https://forums.phpfreaks.com/topic/120298-adding-functions-to-existing-objects/#findComment-619943 Share on other sites More sharing options...
Mchl Posted August 19, 2008 Share Posted August 19, 2008 I usually do an Object class and dbInterface class for it, which deals with fetching data from database, and then instantiates Object and sets its properties. abstract class dbObjectInterface { public static function get('ID') { $data = getDataFromDB(); $object = new Object(); $object->setData($data); return $object; } } Link to comment https://forums.phpfreaks.com/topic/120298-adding-functions-to-existing-objects/#findComment-619949 Share on other sites More sharing options...
knowj Posted August 19, 2008 Share Posted August 19, 2008 Same here I rarly create object within my views. I try work with a rough mvc approach. Link to comment https://forums.phpfreaks.com/topic/120298-adding-functions-to-existing-objects/#findComment-619999 Share on other sites More sharing options...
448191 Posted August 19, 2008 Share Posted August 19, 2008 Hello! I'm a bit new to OOP and am trying to find a way to add functions to already existing objects. I'm pulling tables from a mysql "character" database with mysql_fetch_object, and these objects are organized just how I need them, except they lack the functions normally defined in the "character" class. Thanks! Then you transfer the data to the business object. The objects you fetch with mysql_fetch_object should be seen as Data Transfer Objects. Arrays do the same job, so you could simply use mysql_fetch_assoc as well. Link to comment https://forums.phpfreaks.com/topic/120298-adding-functions-to-existing-objects/#findComment-620007 Share on other sites More sharing options...
verycleanteeth Posted August 19, 2008 Author Share Posted August 19, 2008 Thanks for the suggestions. According to this http://us3.php.net/mysql_fetch_object I should be able to specify a class name as the second parameter to mysql_fetch_object and have the functions automagically added to my database fetched objects, but I could not get it to work properly. I've ended up using the last suggestion and using mysql_fetch_assoc to get an array and then using a constructor method to iterate through each each array item and add it to the object. <?PHP class char { function char($char) { foreach ($char as $key => $value) $this->$key = $value; } } $query = "SELECT * FROM $table WHERE ID IN ($get_char_list)"; $result = mysql_query($query); while ($char = new char(mysql_fetch_assoc($result))) { //do stuff } ?> This seems like a rather dirty workaround. I'd much rather use the mysql_fetch_object function, but this at least works. Link to comment https://forums.phpfreaks.com/topic/120298-adding-functions-to-existing-objects/#findComment-620509 Share on other sites More sharing options...
knowj Posted August 20, 2008 Share Posted August 20, 2008 I personally have never used this method. But if your working with PHP5/MySQL5 I would suggest using mysqli db class rather than the old mysql functions. The mysqli class it supposed to be the replacement for the mysql functions mainly aimed at an OOP environment. Some people however prefer to use the PEAR DB class this is still a realm im yet to explore. Link to comment https://forums.phpfreaks.com/topic/120298-adding-functions-to-existing-objects/#findComment-620900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.