Showing posts with label $this. Show all posts
Showing posts with label $this. Show all posts

Saturday, March 24, 2007

What References Are and What Reference Do

References in PHP are a means to access the same variable content by different names. They are not like C pointers; instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The most close analogy is with Unix filenames and files - variable names are directory entries, while variable contents is the file itself. References can be thought of as hardlinking in Unix filesystem.

PHP references allow you to make two variables to refer to the same content. Meaning, when you do:


<?php
$a =& $b;
?>

it means that $a and $b point to the same content.
Note: $a and $b are completely equal here, that's not $a is pointing to $b or vice versa, that's $a and $b pointing to the same place.
Unsetting References

When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed. For example:

<?php
$a = 1;
$b =& $a;
unset($a);
// OR
$a = 1;
$b =& $a;
$b = NULL;
?>

won't unset $b, just $a.
Again, it might be useful to think about this as analogous to Unix unlink call.

$thisIn an object method, $this is always a reference to the caller object.

PHP5 static property and static function


<?php
class Visibility
{
public $a = "a";
// var is synmic of public.
protected $b = "b";
private $c = "c";
const T = "const";
public static $s = "static";

public function public_func()
{
echo "public function";
echo "<br>";
}

protected function protected_func()
{
echo "protected function";
echo "<br>";
}

private function private_func()
{
echo "private function.";
echo "<br>";
}

public function static_var()
{
echo self::$s;
echo " self::\$s<br><br>";
}

public static function static_func()
{
echo "static function.";
echo "<br>";
echo "can't use \$this.";
echo "<br>";
// echo $this->a;
// Fatal error: Using $this when not in object context
}
}

$v = new Visibility();
echo $v->a;
// echo $v->b; // Fatal error
// echo $v->c; // Fatal error
echo "<br>";
$v->public_func();
// $v->protected_func(); // Fatal error
// $v->private_func(); // Fatal error
// echo Visibility::$a;
// Fatal error: Access to undeclared static property: Visibility::$a
echo Visibility::$s;
echo "<br>";
// echo Visibility::s; //Undefined class constant 's'
echo Visibility::T;
echo "<br>";
echo $v->static_var();
// echo $v->s; // output nothing.
// Static properties cannot be accessed through the object using the arrow operator ->.
$v->static_func(); // Static methods can be accessed through the object using ->.
Visibility::static_func();
// Because static methods are callable without an instance of the object created,
// the pseudo variable $this is not available inside the method declared as static.
echo "<br>==============================<br>";
?>

<?php
class ExVisibility extends Visibility
{
public static $s = "e static";
public function ex_func()
{
echo parent::$s;
echo " parent::\$s<br>";
echo self::$s;
echo " self \$s<br>";
}

}
$e = new ExVisibility();
$e->ex_func();
echo ExVisibility::$s;
echo "<br />";

?>