umbrella_thing Posted December 6, 2006 Share Posted December 6, 2006 Hi Dudes and Dudets,I have a questiong relating to PHP5 classes, I want to be able to use the methods and members of a class, inside another class!For instance, a class that access the database. I would want to use this inside a perhaps an article class...I know you can "include" a class and do Name::method(), but I'm not sure this is the best way...is this possbile...[code]private dbConnection = new DataAccesspublic function getArticlesById($id) {$result = dbConnection->query("select * from article where id = $id");}[/code]I don't know if this makes sense to anyone? Quote Link to comment Share on other sites More sharing options...
willfitch Posted December 6, 2006 Share Posted December 6, 2006 Accessing members and methods of classes using the Scope Resolution Operator ( :: ) is not a bad way, when you're referencing a parent or static member. The best way is through a form of polymorphism. For instance, if I were to want access to the MySQLi class (build into PHP http://us3.php.net/mysqli), I could do so like this:[code=php:0]<?php$mysqli = new mysqli('host','user','pass','db');class myclass { private $db; public function __construct($db) { $this->db = $db; } public function __destruct() { $this->db->close(); }}$test = new myclass($mysqli);?>[/code]This will give you access within any class. Another way would be this:[code=php:0]<?phpclass myclass { private $db; public function __destruct() { $this->db->close(); } public function __construct() { $this->db = new mysqli('host','user','pass','db'); }}?>[/code]In the above example, the external class is actually instantiated within the class. The final way would be to acually extend the class. Quote Link to comment Share on other sites More sharing options...
keeB Posted December 6, 2006 Share Posted December 6, 2006 [code]<?phpabstract class mysql_database { public $connection; public function mysql_database($host="localhost", $user="", $pass="", $db="") { $this->connection = mysql_connect($host, $user, $pass); if($db != "") { $this->select_db($db, $this->connection); } } public function select_db($db) { if($db != "") { mysql_select_db($db, $this->connection); } else { die ("please select a database."); } } public function query($q,$mode=MYSQL_ASSOC) { $retVal = array(); $result = @mysql_query($q) or die("Invalid query: $q"); while($row = @mysql_fetch_array($result,$mode)) { array_push($retVal, $row); } return $retVal; } public function queryCount($q) { $result = $this->query($q); $row = mysql_fetch_array($result, MYSQL_ASSOC); return $row[0]; }}?> [/code]Thats my mysql class.If you wanted to add functionality to it, you would:[code]<?phpclass DataAccess extends mysql_database }?>[/code]You can use the BASE class methods in the new class, etc.Good luck, let me know if you need any more help - Keeb Quote Link to comment Share on other sites More sharing options...
umbrella_thing Posted December 6, 2006 Author Share Posted December 6, 2006 Thanks guys for your help!"extending" to other classes works really well. Very "OO".Thanks again :) Quote Link to comment Share on other sites More sharing options...
willfitch Posted December 7, 2006 Share Posted December 7, 2006 keeB,Any reason you don't like MySQLi? I see you are using abstract classes, so PHP5 is your version. I'm sure if you're running PHP5 and have MySQL extensions, MySQLi is very likely available.Unless you are currently using persistant connections, I would look into the MySQLi extension.Also, the danger of using an abstract class for your DB mechanism is that you can't instantiate it. It must be extended. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.