Syntax to call a class' methods in your own class
| From: | Graham Anderson | Date: | Wed, 24 May 2006 21:52:44 +0000 |
| Subject: | Syntax to call a class' methods in your own class | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-236856@lists.php.net to get a copy of this message | ||
What is the correct syntax for calling methods from other classes into your own class ?
Example:
Calling the ADODB class with:
$this->conn=&ADONewConnection('mysql');
This works:
$rs= $this->conn->GetAll("SELECT title FROM content WHERE page_id= ?",$id);
This fails:
while (!$rs->$this->conn->EOF)
{
// iterate through $rs
}
Can someone point me in the right direction ?
I did not want to use 'Extends' because I wanted incorporate several pre-existing classes,like ADODB, into my own class
I am more than a bit new at OOP so any help is appreciated.
Code:
<?php
require ("./adodb/adodb.inc.php");
$PageBuilder = new PageBuilder();
$PageBuilder->getPageTextbyID($id=1);
//------------------------------------------------------
class PageBuilder{
public function __construct() {
$this->connect2db('localhost','u','p','db');
}
public function __destruct() {
# Close the db connection
$this->conn->Close();
}
//---- Methods within the PageBuilder Class ---- //
private function connect2db()
{
$this->conn=&ADONewConnection('mysql');
$this->conn->Connect($server,$u,$p,$db);
}
public function getPageTextbyID($id)
{
// this query DOES work
$rs= $this->conn->GetAll("SELECT title FROM content WHERE page_id= ?",$id);
// ---------------Now iterate through $rs----------------//
// Original Iteration Code
while (!$rs->EOF) {
for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++)
print $result->fields[$i].' ';
$rs->MoveNext();
print "<br>\n";
// Failed: First Attempt using $this->while (!$rs->$this->conn->EOF) { for ($i=0, $max=$rs->$this->conn->FieldCount(); $i < $max; $i++) echo "the result is:". $rs->$this->conn->fields[$i].' '; $rs->$this->conn->MoveNext(); print "<br>\n"; } // Failed: Second Attempt using :: while (!$rs->conn::EOF) { for ($i=0, $max=$rs->conn::FieldCount(); $i < $max; $i++) echo $rs->conn::fields[$i].' '; $rs->conn::MoveNext(); print "<br>\n"; } }