Instantiation
A class is only a blueprint, it must be instantiated into object before use. For instantiation use new constructor keyword.
class myClass
{
// Class contents go here
}
$myClassInstance = new myClass();
Reference
An object is always passed by reference (different from others type of variables). For Object & are not needed.
class A
{
public $foo = 1;
}
$a = new A;
$b = $a; // both $a and $b will point to the same object
echo $a->foo; // 1
$a->foo = 2;
echo $b->foo; // 2 - Look Here
Reinstantiation
Object reinstantiation breaks the reference.
class myClass
{
public $foo;
public function __construct($val)
{
$this->foo = $val;
}
}
$a = new myClass(1);
$b = $a;
echo $a->foo; // 1
$a->foo = 2;
echo $b->foo; // 2
$a = new myClass(2); // Look Here
echo $a->foo; // 2
echo $b->foo; // 1 -> reference is broken
Inheritance
Class Inheritance allows a class to extend another class. Parent classes can be accessed using the special parent:: namespace.
class A
{
function test()
{
echo "A";
}
}
class B extends A
{
function test()
{
parent::test(); // A
echo "B";
}
}
$a = new A();
$b = new B();
$a->test(); // A
$b->test(); // AB
Last update: 402 days ago