That's incorrectly syntactically. Won't run.
----
Ok, let's write some code to show how this can be done:
class SearchHelper extends AjaxACApplication {
private $dbh;
public function __construct() {
$this->dbh = $GLOBALS['dbh'];
}
}
----
Better yet, pass the database host in the construction:
class SearchHelper extends AjaxACApplication {
private $dbh;
public function __construct($dbh) {
$this->dbh = $dbh;
}
}
$searchHelper = new SearchHelper($dbh);
----
Even better, DON'T use globals:
class DatabaseConnectionInfo {
private var $host;
...
public function getHost() {
return $this->host();
}
...
private static $defaultConnectionInfo;
public static function getDefault() {
if (self::$defaultConnectionInfo === null) {
...build default connection...
}
return self::$defaultConnectionInfo;
}
}
class SearchHelper extends AjaxACApplication {
private $dbh;
public function __construct(DatabaseConnectionInfo $connectionInfo = null)
{
if ($connectionInfo === null) {
$connectionInfo = DatabaseConnectionInfo::getDefault();
}
$this->dbh = $connectionInfo->getHost();
}
}
Yes, I know what you're thinking, too much more code for just a stupid
thing. Trust me, refactoring it's easier when the code was written right the
first time.
2007/9/30, 潘志彬 <[EMAIL PROTECTED]>:
>
> $dbh = 'test';
>
> class search_helper extends AjaxACApplication
> {
> /**
> * Database connection details
> */
>
> // announce global variable before use it
> global $dbh;
>
> $db_hostname = $dbh;
>
>
> ...
>
> Regards,
> Ryu
>
>
>
> 2007/9/29, Merlin <[EMAIL PROTECTED]>:
> >
> > Hi there,
> >
> > I am new to PHP classes and I do want to access a variable outside the
> > class, but somehow that does not work. global also does not have any
> > effect.
> >
> > In the following example I would like to be able to access $dbh from
> > inside the class like I did in that example. This does not work. Can
> > somebody please give me a hint on the right syntax?
> >
> > $dbh = 'test';
> >
> > class search_helper extends AjaxACApplication
> > {
> > /**
> > * Database connection details
> > */
> >
> > var $db_hostname = $dbh;
> >
> > Thank you for any help,
> >
> > Merlin
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>