clone
function clones the object and __clone
method is going to call when the object gets cloned class User {
public $username;
public $friends = ['Tom','David'];
function __construct($name) {
$this->username = $name;
print $this->username . "'s object is created'\n";
}
function __clone() {
print $this->username . "'s object is cloned'\n";
}
}
$user1 = new User('Tom');
$user2 = clone $user1;
public
- visible from anywhere
private
- visible only for the owner class
protected
- visible only for the owner and it's child classes
class User {
public $username;
public $friends = ['Tom','David'];
function __construct($name) {
$this->username = $name;
print $this->username . "'s object is created'\n";
}
private function მარტივიჯამი($num1,$num2) {
return $num1 + $num2;
}
}
class Admin extends User{
function sumNumbers($num1,$num2) {
return $this->მარტივიჯამი($num1,$num2);
}
}
$admin1 = new Admin('Tom');
echo $admin1->sumNumbers(4,5);
class User {
public function მარტივიჯამი($num1,$num2) {
return $num1 + $num2;
}
}
class Admin extends User{
}
$admin1 = new Admin('Tom');
echo $admin1->მარტივიჯამი(4,5);
class BaseClass {
function __construct() {
print "In BaseClass constructor\n";
}
}
class SubClass extends BaseClass {
function __construct() {
parent::__construct();
print "In SubClass constructor\n";
}
}
class OtherSubClass extends BaseClass {
// inherits BaseClass's constructor
}
// In BaseClass constructor
$obj = new BaseClass();
// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();
// In BaseClass constructor
$obj = new OtherSubClass();
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "\n";
}
}
About abstract class:
Classes defined as abstract cannot be instantiated
Any class that contains at least one abstract method must also be abstract
Methods defined as abstract simply declare the method's signature - they cannot define the implementation
Circle
implements CircleAbstract
interface.
Therefore it must define getArea()
and getPerimeter()
methods interface CircleAbstract {
// Force Extending class to define these methods
function getArea();
function getPerimeter();
}
class Circle implements CircleAbstract {
static $PI = 3.1415926536;
public $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getPI() {
return self::$PI;
}
public function getArea() {
return pow($this->radius, 2) * self::$PI;
}
public function getDiameter() {
return $this->radius * 2;
}
public function getPerimeter() {
return $this->radius * 2 * self::$PI;
}
}
$circle1 = new Circle(5);
echo "\nPI: " . $circle1->getPI();
echo "\nArea: " . $circle1->getArea();
echo "\nDiameter: " . $circle1->getDiameter();
echo "\nPerimeter: " . $circle1->getPerimeter();
interface int1 {
// Force Extending class to define these methods
function getArea();
}
interface int2 {
function getPerimeter();
}
class Circle implements int1, int2 {
static $PI = 3.1415926536;
public $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getPI() {
return self::$PI;
}
public function getArea() {
return pow($this->radius, 2) * self::$PI;
}
public function getDiameter() {
return $this->radius * 2;
}
public function getPerimeter() {
return $this->radius * 2 * self::$PI;
}
}
$circle1 = new Circle(5);
echo "\nPI: " . $circle1->getPI();
echo "\nArea: " . $circle1->getArea();
echo "\nDiameter: " . $circle1->getDiameter();
echo "\nPerimeter: " . $circle1->getPerimeter();