Using $this->var or self::var
Posted: Mon Aug 14, 2006 4:03 am
Is there any difference between using $this->var or self::var? Do you have an preference between the two? Any reason why?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
class A
{
private foo;
private static bar = 5;
public function __construct ($var)
{
$this->foo = $var;
}
public static useBar ()
{
return self::bar;
}
}Code: Select all
<?php
class dbcon {
private static $instance = false;
private $linkid = null;
private function __construct() {
if (!self::linkid = @mysql_connect(MYSQL_DATABASE_HOST, MYSQL_USERNAME, MYSQL_PASSWORD))
{
new fatal_exception("FATAL: Database connection failure: " . mysql_errno() . ': ' . mysql_error());
}
}
public static function get_db_link()
{
if ($instance === false)
{
self::$instance = new dbcon;
}
return self::linkid;
}
public function select_db($db = MYSQL_DATABASE_NAME) {
if (!@mysql_select_db($db, self::linkid))
{
new fatal_exception("FATAL: Database selection failure: " . mysql_errno() . ': ' . mysql_error());
}
}
}
?>Code: Select all
class dbcon {
private static $instance = false;
private $linkid = null;
private function __construct() {
if (!$this->linkid = @mysql_connect(MYSQL_DATABASE_HOST, MYSQL_USERNAME, MYSQL_PASSWORD))
{
new fatal_exception("FATAL: Database connection failure: " . mysql_errno() . ': ' . mysql_error());
}
}
public static function get_db_link()
{
if ($instance === false)
{
self::$instance = new dbcon;
}
return self::$instance->getLink();
}
public function getLink ()
{
return $this->linkid;
}
public function select_db($db = MYSQL_DATABASE_NAME) {
if (!@mysql_select_db($db, $this->linkid))
{
new fatal_exception("FATAL: Database selection failure: " . mysql_errno() . ': ' . mysql_error());
}
}
}