William Piper wrote:
> 
> 
> [EMAIL PROTECTED] <mailto:listgroups08%40ozwebwiz.com> wrote:
>  >
>  >
>  > Hi all,
>  > I am playing with classes for the first time and I am at a bit of a
>  > loss in php.
>  >
>  > How do I access the properties/functions of the parent class from sub
>  > function?
>  >
>  > And how do I register resources into the scope of the parent class?
>  >
>  > For example if I have -
>  > class Data_Base
>  > {
>  > var $server;
>  > var $username;
>  > var $password;
>  > function Data_Base
>  > {
>  > $this->server = "127.0.01";
>  > $this->username = "username";
>  > $this->password = "password";
>  > }
>  > function Connect
>  > {
>  > $handle = mysql_connect($server, $username, $password);
>  > }
>  > }
>  >
>  > How do I get $server etc from the root scope of Data_Base?
>  >
>  > And how do I register $handle into the global scope of Data_Base without
>  > registering it into the global scope of PHP?
>  >
>  > ie - does var $server create a scope that is accessible in sub 
> functions or
>  > is it just in the root scope?
>  >
>  > Thanks, Robert.
> 
> Do this:
> class Data_Base
> {
> var $server;
> var $username;
> var $password;
> var $handle;
> 
> function Data_Base
> {
> $this->server = "127.0.01";
> $this->username = "username";
> $this->password = "password";
> }
> function Connect
> {
> $this->handle =
> mysql_connect($this->server,$this->username,$this->password);
> }
> }
> 
> Now, you can run queries like this anywhere you want:
> mysql_query("SELECT * FROM blah",$this->handle);
> 

Oh, I forgot to mention, "$this" only works in the class scope. If you 
are calling the methods from outside, you would need to refer it via the 
class name variable. For example:

<?php
include('mydbclass.php');
$dbclass = new Data_Base();
$dbclass->Connect();
mysql_query('SELECT * FROM blah',$dbclass->handle);
?>

Reply via email to