Re: Syntax to call a class' methods in your own class
| From: | Stephen Lake | Date: | Wed, 24 May 2006 23:19:34 +0000 |
| Subject: | Re: Syntax to call a class' methods in your own class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-236865@lists.php.net to get a copy of this message | ||
Here's a link to a series of 4 articles that explain aggregation in
PHP.....as this sounds like what you want to do.
http://www.devshed.com/c/a/PHP/Object-Interaction-in-PHP-Introduction-to-Aggregation-part-1/
HTH
Steve
"Graham Anderson" <grahama@siren.cc> wrote in message
news:D8BBF5FC-1FBD-4428-AB59-166B4F4A38F4@siren.cc...
> 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";
> }
>
>
> }