Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
class Foo
{
    public static $staticVariable = 'foo';

    public static function getVariableStatic() {
        return self::$staticVariable;
    }

    public function getVariableNONStatic() {
        return self::$staticVariable;
    }
}
We can not access
$staticVariable
using an object directly:
$foo = new Foo();
print $foo->staticVariable;
This will produce the following error message:
Notice:  Accessing static property Foo::$staticVariable as non static...
But there are several ways to access static variable using an object: 1. Using static getter method:
$foo = new Foo();
print $foo->getVariableStatic();
2. Using non-static getter method:
$foo = new Foo();
print $foo->getVariableNONStatic();
php.net:
A property declared as static cannot be accessed with an instantiated class object (though a static method can).
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP official doc
2
Type hinting means to define which class object we want to receive exactly as a parameter in a constructor
class BLock { }
class Lock {
    private $isLocked;
    
    public function __construct() {
        
    }
    
    public function lock() {
        $this->isLocked = true;
    }
    
    public function unLock() {
        $this->isLocked = false;
    }
    
    public function isLocked() {
        return $this->isLocked;
    }
}
class Chest {
    private $lock;
    
    public function __construct(Lock $lock) {
        $this->lock = $lock;
    }
    
    public function close() {
        $this->lock->lock();
        echo 'Closed' . PHP_EOL;
    }
    
    public function open() {
        if ($this->lock->isLocked()) {
            $this->lock->unLock();
        }
        echo 'Opened' . PHP_EOL;
    }
    
    public function isClosed() {
        return $this->lock->isLocked();
    }
}

$chest = new Chest(new Lock);
// $chest = new Chest(new Block);

$chest->open();
$chest->close();
In this example
Chest
class constructor waits to receive
Lock
class object. If we pass any other class object, we will get an error like the following: Fatal error:
Uncaught TypeError: Argument 1 passed to Chest::__construct() must be an instance of Lock, instance of BLock given
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
OOP PHP
2
In this example the class constructor receives only
int
parameter. If any other type is passed, fatal error will be generated
class Book {
    public $price;
    
    public function price(int $price) {
        $this->price = $price;
    }
}

$book = new Book;

$book->price('k34');

echo $book->price;
Fatal error:
Uncaught TypeError: Argument 1 passed to Book::price() must be of the type int, string given
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
OOP PHP
2
__destruct
method will be executed at the end (before the script stops execution)
class Bill {
    public $dinner = 20;
    public $dessert = 5;
    public $drink = 3;
    
    public $bill = 0;
    public function __construct() {
        $this->bill = 10;
    }
    public function dinner($count) {
        $this->bill += $count * $this->dinner;
        return $this;
    }
    public function dessert($count) {
        $this->bill += $count * $this->dessert;
        return $this;
    }
    public function drink($count) {
        $this->bill += $count * $this->drink;
        return $this;
    }
    public function __destruct() {
        echo $this->bill;
    }
}

$bill = new Bill;

$bill->dinner(3)->dessert(2)->drink(1);
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
OOP PHP
2
Method chaining technique gives us ability to call several methods with only one PHP expression (in one go)
class Bill {
    public $dinner = 20;
    public $dessert = 5;
    public $drink = 3;
    
    public $bill = 0;
    
    public function dinner($count) {
        $this->bill += $count * $this->dinner;
        return $this;
    }
    public function dessert($count) {
        $this->bill += $count * $this->dessert;
        return $this;
    }
    public function drink($count) {
        $this->bill += $count * $this->drink;
        return $this;
    }
}

$bill = new Bill;
We can call several methods one by one and print the
$bill
:
$bill->dinner(3); 
$bill->dessert(2);
$bill->drink(1);
echo $bill->bill;
Or we can combine the above 4 expressions using method chaining. The result will be the same:
echo $bill->dinner(3)->dessert(2)->drink(1)->bill;
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
OOP PHP
2
Content of
index.php
file
spl_autoload_register(function($class) {
    // echo 'register class:'.$class."<br>";
    require_once("classes/{$class}.php");
});

// echo 'hey there on line 7'."<br>";

$cat = new Cat;
$dog = new Dog;
$tortoise = new Tortoise;

echo $cat->talk();
echo $dog->talk();
echo $tortoise->talk();
Content of
classes/Talkative.php
file
interface Talkative {
    public function talk();
}
Content of
classes/Cat.php
file
class Cat implements Talkative {
    public function talk() {
        return 'Meow' . '<br>';
    }
}
Content of
classes/Dog.php
file
class Dog implements Talkative {
    public function talk() {
        return 'Woof' . '<br>';
    }
}
Content of
classes/Tortoise.php
file
class Tortoise implements Talkative {
    public function talk() {
        return 'Yak yak yak yak ...' . '<br>';
    }
}
Filenames and class names must be THE SAME
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
1
class Say {
    public function let() {
        echo 'Let' . " ";
        return $this;
    }
    public function me() {
        echo 'Me' . " ";
        return $this;
    }
    public function tell() {
        echo 'Tell' . " ";
        return $this;
    }
    public function you() {
        echo 'You' . " ";
        return $this;
    }
    public function something() {
        echo 'Something' . " ";
    }
}
$phrase = new Say;
$phrase->let()->me()->tell()->you()->something();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
3
It is not possible to use
$this
inside static methods
class Greeting {
    public $nonStatic = 'some value';
    
    public static function hello() {
        return $this->nonStatic;
    }
}

// echo Greeting::hello();

$obj1 = new Greeting();
echo $obj1->hello();
After executing the above code, following error will be generated:
Fatal error:  Uncaught Error: Using $this when not in object context
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
3
class Foo {
    public function __toString()
    {
        return "Some text about the OBJECT"; 
    }
}
$foo = new Foo();
echo $foo;
__toString
method gets invoked when we echo or print the object
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
2
class Foo {
    
    public function __call($function, $arguments)
    {
        return "$function has been called with arguments: " . implode(', ', $arguments); 
    }
    
    // public function fireFunction($one, $two, $three) {
    //     return "pre-defined method";
    // }
}
$foo = new Foo();
echo $foo->fireFunction(5, 47, "third argument");
__call
is invoked when the method is inaccessible (when it's not public or does not exist at all)
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
3
Results: 1578