Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
we can use static properties inside object methods (non-static methods)
We can use static properties inside non-static methods but we can't use
$this
inside static methods
class Circle {
    public static $pi = 3.1415926535;
    public $radius;
    
    public function __construct($radius) {
        $this->radius = $radius;
    }
    
    public function getArea() {
        return pow($this->radius, 2) * self::$pi;
    }
    
    public function getPerimeter() {
        return $this->radius * 2 * self::$pi;
    }
}

$circle1 = new Circle(5);
echo "\nArea: " . $circle1->getArea();
echo "\nPerimeter: " . $circle1->getPerimeter();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
3
we can not use $this (object properties) inside static methods
class Wheather {
    public $nonStatic = 0;
    public static $tempConditions = ['cold', 'mild', 'warm'];
    public static $someProperty = 5;

    static function celsiusToFarenheit($celsius) {
        return $celsius * 9 / 5 + 32 + $this->nonStatic;
    }
}

echo Wheather::celsiusToFarenheit(0);
echo "\n";
In this case we will get the error:
Fatal error:  Uncaught Error: Using $this when not in object context
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
3
Use static property inside and outside of a class
Use static property inside a class
echo self::$someProperty;
Outside a class
Wheather::$someProperty;
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
call static method inside and outside of a class
Call the method inside a class
self::celsiusToFarenheit(20);
Outside a class
Wheather::celsiusToFarenheit(20);
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
If there is no constructor in child class, then parent class constructor will be called (when appropriate parameters are passed)
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();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP official doc
1
We can call parent class constructor from child class using
parent
keyword
parent::__construct($name);
Complete example:
class User {
    public $username;

    function __construct($name) {
        $this->username = $name;
    }
}
class Admin extends User{
    public $admin_level;
    
    function __construct($name, $admin_level) {
        parent::__construct($name);
        $this->admin_level = $admin_level;
    }
}
$admin1 = new Admin('Tom', 'Manager');

echo $admin1->username;
echo "\n";
echo $admin1->admin_level;
If there is no constructor in child class, then parent class constructor will be called (when appropriate parameters are passed)
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
2
The child class extends parent class means that it borrows properties and methods from its parent
class User {
    public function მარტივიჯამი($num1,$num2) {
        return $num1 + $num2;
    }
}
class Admin extends User{
    
}
$admin1 = new Admin('Tom');

echo $admin1->მარტივიჯამი(4,5);
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
Main benefit of using getters and setters is to filter & check values according to current needs
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
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);
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
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;
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
Object Oriented PHP Tutorial
1
Results: 1578