Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
__get
and
__set
magic methods allows us to get and set any variable without having individual getter and setter methods:
class Foo {
    protected $properties = array();

    public function __get( $key )
    {
        if(array_key_exists($key, $this->properties)){
            return $this->properties[$key];
        }
        return $this->properties[ $key ];
    }
    public function __set( $key, $value )
    {
        $this->properties[ $key ] = $value;
    }
}
$foo = new Foo();
$foo->name = 'David';

echo $foo->name;
In this example, we can set any variable, even if it does not exist. Every variable will be addd to
properties
array
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
3
__get
and
__set
methods are called when there is an attempt either to use or to set a new value to any of the class property
class Person{
    public $firstName;
 
    public function __get($propertyName){
        echo "attempted to read property: $propertyName" . PHP_EOL;
    } 
    public function __set($propertyNane, $propertyValue){
        echo "attempted to set new value to property: $propertyNane" . PHP_EOL;
    } 
}
 
$p = new Person();
 
$p->firstName = 'Doe';
echo $p->firstName . PHP_EOL;
 
$p->lastName = 'John';
echo $p->lastName;
If the property is
public
, the methods are not going to be called (if the property does not exist, or exists but is not public, the methods will get called) If we make
$firstName
property
private
or
protected
, the methods will be called for the
$firstName
property
__get
,
__set
,
__call
and
__callStatic
are invoked when the method or property is inaccessible
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
3
interface Talkative {
    public function talk();
}
class Cat implements Talkative {
    public function talk() {
        return 'Woof' . PHP_EOL;
    }
}
class Dog implements Talkative {
    public function talk() {
        return 'Meow' . PHP_EOL;
    }
}
class Tortoise implements Talkative {
    public function talk() {
        return 'Yak yak yak yak ...' . PHP_EOL;
    }
}

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

echo $cat->talk();
echo $dog->talk();
echo $tortoise->talk();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
2
Serializes the object to a value that can be serialized natively by
json_encode()
class MyClass implements JsonSerializable {
    public function jsonSerialize() {
        return json_encode([1, 2, "44"]);
    }
}
$c = new MyClass();
echo json_encode($c);
In this example after calling
json_encode($c)
function,
JsonSerializable
will be called automatically because the class
MyClass
implements
JsonSerializable
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
2
Classes implementing Countable can be used with the count() function
class MyClass implements Countable {
    public function count() {
        return 5;
    }
}
$c = new MyClass();
echo $c->count();
echo count($c); //calls $c->count();
We can use count() function for the class instance, when the class implements
Countable
interface
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
2
Dependency injection is a technique when we pass one class object to another class constructor:
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) {
        $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->open();
$chest->close();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
2
class Bird{
    public $canFly;
    public $legCount;
    public $className;
    
    public function __construct($canFly, $legCount, $className) {
        $this->canFly = $canFly;
        $this->legCount = $legCount;
        $this->className = $className;
    }
    
    public function canFly() {
        return $this->canFly;
    }
    
    public function getLegCount() {
        return $this->legCount;
    }
    
    public function getBirdInfo() {
        $canFly = $this->canFly() ? 'can fly' : 'can not fly';
        return $this->className. ' ' . $canFly . ' and has ' . $this->getLegCount() . ' legs' . PHP_EOL;
    }
}

class Pigeon extends Bird{
    public function __construct($canFly, $legCount) {
        parent::__construct($canFly, $legCount, get_class());
    }
}

class Penguin extends Bird{
    public function __construct($canFly, $legCount) {
        parent::__construct($canFly, $legCount, get_class());
    }
}

$pigeon = new Pigeon(true, 2);
$penguin = new Penguin(false, 2);

echo $pigeon->getBirdInfo();
echo $penguin->getBirdInfo();
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
2
1.
json_decode
returns an instance of
stdClass
class
$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);


//Example with StdClass
echo get_class($stdInstance) . ':' . PHP_EOL;
echo $stdInstance->foo . PHP_EOL; //"bar"
echo $stdInstance->number . PHP_EOL . PHP_EOL;


//Example with associative array
echo 'associative array:' . PHP_EOL;
$array = json_decode($json, true);
echo $array['foo'] . PHP_EOL; //"bar"
echo $array['number'] . PHP_EOL; //42
2. Casting an array to an object returns an instance of
stdClass
class
$array = array(
    'Property1'=>'hello',
    'Property2'=>'world',
    'Property3'=>'again',
);

$obj = (object) $array;
echo get_class($obj) . ':' . PHP_EOL;
echo $obj->Property3;
User details info represented using an array and an object
$array_user = array();
$array_user["name"] = "smith john";
$array_user["username"] = "smith";
$array_user["id"] = "1002";
$array_user["email"] = "smith@nomail.com";

$obj_user = new stdClass;
$obj_user->name = "smith john";
$obj_user->username = "smith";
$obj_user->id = "1002";
$obj_user->email = "smith@nomail.com";
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
1
Polymorphism is - when multiple classes have different functionality but they all share common interface
interface Element {
    public function characteristics();
}
class Water implements Element {
    public function characteristics() {
        return [
            'Water characteristic 1',
            'Water characteristic 2',
            'Water characteristic 3',    
        ];
    }
}
class Fire implements Element {
    public function characteristics() {
        return [
            'Fire characteristic 1',
            'Fire characteristic 2',
            'Fire characteristic 3',    
        ];
    }
}
class Air implements Element {
    public function characteristics() {
        return [
            'Air characteristic 1',
            'Air characteristic 2',
            'Air characteristic 3',    
        ];
    }
}
class Earth implements Element {
    public function characteristics() {
        return [
            'Earth characteristic 1',
            'Earth characteristic 2',
            'Earth characteristic 3',    
        ];
    }
}


function describe(Element $element) {
    echo get_class($element) . "\n";
    $characteristics = $element->characteristics();
    if (is_array($characteristics)) {
        foreach ($characteristics as $characteristic) {
            echo $characteristic . "\n";
        }
        echo "\n\n";
    }
}

$element = new Water;
describe($element);

$element = new Fire;
describe($element);

$element = new Air;
describe($element);

$element = new Earth;
describe($element);
by Valeri Tandilashvili
5 years ago
0
PHP
OOP
1
With quotes:
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
Without quotes:
$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; // &lt;a href='test'&gt;Test&lt;/a&gt;
The second parameter possible values:
ENT_COMPAT
- Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES
- Will convert both double and single quotes.
ENT_NOQUOTES
- Will leave both double and single quotes unconverted. The default is ENT_COMPAT
by Valeri Tandilashvili
5 years ago
0
PHP
functions
Object Oriented PHP Tutorial
1
Results: 1578