MENU
Scope
A class often extends another class and inherits its members. The child may override a property or method from the parent class (using the same arguments and visibility signature). Methods declared final must not be overridden, whereas final classes must not be inherited.Properties and method declared public are accessible everywhere. If they are declared as private, they are accessible only within the class and are not inheritable. If they are declared protected, they are only accessible within the class, any inheriting classes, and any inherited classes. If undeclared, a method in a class is public by default.
Properties, when being initialized, must take constant values, instead of other variables or expressions.
$this-> is used within a class method to represent the current object.
The properties of an object can be looped through with foreach.
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class P{
public $v1=1;
protected $v2=2;
private $v3=3;
function smile(){ // accessible everywhere
echo $this->v1.")Smile, Universe!<br />";
}
final protected function laugh(){ // inheritable
echo $this->v3.")Laugh, Earth!<br />";
}
private function shout(){ // accessible within P only
echo "Hello World! <br />";
}
}
final class C extends P{ // cannot extend C due to 'final'
function smile(){ // method overriding
echo $this->v2.")Smile, World!<br />";
}
//protected function laugh(){} // error due to 'final'
}
$p = new P();
$c = new C; // ()are optional when creating an instance with new
$p->smile();
$c->smile();
$p->shout(); // error
?></body>
</html>
1)Smile, Universe! 2)Smile, World! Fatal error: Call to private method P::shout() from context '' in E:\Program Files\xampp\htdocs\intro.php on line 33
$this-> is used within a class method to represent the current object.
RESETRUNFULL
Two objects are equal (==) if they belong to the same class, and their properties have equal values. Two objects are identical (===) only if they refer to the same instance. The instanceof operator tests if an object belongs to a class.RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class MyClass{
public $a=1;
public $b=2;
private $c=3;
function f(){
foreach ($this as $key => $value)
echo $key.":".$value."<br />";
}
}
$Obj = new MyClass;
foreach ($Obj as $key => $value){ // $c not printed
echo $key.":".$value."<br />";
}
$Obj->f(); // $c printed
?></body>
</html>
If two objects are of the same class, they will have access to each other’s private and protected members.
$this-> is used within a class method to represent the current object.
RESETRUNFULL
When an object’s variable is assigned to another variable, or passed into a function as an argument, the new variable points to the same object instance. Hence any change made to the one variable affects the other. This form of assignment by reference, is different from the assignment by value form used in the assignment of scalar values and arrays. Use clone to get another copy of the object.
RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class C{
private $a=3;
function f($o){
echo $o->a; // accessible even if it is private
}
}
$v1=new C;
$v2=new $v1;
echo ($v1==$v2); // 1,true
echo ($v1===$v2); // NULL, false
echo ($v2 instanceof C); // 1, true
$v1->f($v2); // 3
?></body>
</html>
$this-> is used within a class method to represent the current object.
RESETRUNFULL
RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class MyClass{
public $v=1;
}
$a= new MyClass;
$b=$a; $a->v=2; echo $b->v;
$c=&$a;$a->v=3; echo $c->v;
function f($o){$o->v=4;}f($a);echo $a->v;
$d=clone $a; $a->v=5; echo $d->v;
$a=NULL; echo $b->v;
echo $c->v; // error
?></body>
</html>