1. Go to this page and download the library: Download mekramy/oop-util library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mekramy / oop-util example snippets
class Person
{
# Use trait
use \MEkramy\OOPUtil\MapGetterSetter;
# class states
private $fn;
private $ln;
# First name getter/setter
public function setFirstName(string $firstName): void
{
$this->fn = $firstName;
}
public function getFirstName(): string
{
return $this->fn;
}
# Last name getter/setter
public function setLastName(string $lastName): void
{
$this->ln = $lastName;
}
public function getLastName(): string
{
return $this->ln;
}
# Full name getter only
public function getFullName(): string
{
return "{$this->fn} {$this->ln}";
}
}
$person = new Person();
# All property access is valid
$person->first_name = 'John';
$person->firstName = 'John';
$person->FirstName = 'John';
$person->lastName = 'Doe';
echo $person->full_name;
# This line throw error because full_name has no setter method
$person->full_name = 'John Doe'; # throws InvalidArgumentException
# This line throw error because no getter/setter defined for notExistsProperty
$person->notExistsProperty = 'Oops!';
class Person
{
# class body ...
private $extra = [];
# Handle undefined getters
protected function __onGetFailed($name){
return array_key_exists($name, $this->extra) ? $this->extra[$name] : null;
}
# Handle undefined setters
protected function __onSetFailed($name, $value): void
{
# disallow set full_name
if(!in_array($name, ['full_name', 'fullName', 'FullName'])){
$this->extra[$name] = $value;
}
}
}
class MyClass{
use \MEkramy\OOPUtil\CanChained;
/**
* Methods list to
return ['doFirst', 'doSecond', 'doThird'];
}
/**
* Methods list to exclude in chaining call
*
* @return array
*/
protected function __cantChain(): array
{
return ['getResult'];
}
public function doFirst(string $input){ ... }
public function doSecond(string $input){ ... }
public function doThird(string $input){ ... }
public function getResult(): string{ ... }
}
$instance = new MyClass();
$instance->chaining()->doFirst('first')->doSecond('second')->doThird('third');
# this line throws error because getResult method not chainable
$instance->chaining()->doFirst('first')->getResult();
# A: By calling normally without chaining
$instance->getResult();
# B: By calling getInstance method on chaining call
$instance->chaining()->doFirst('first')->getInstance()->getResult();
class Dummy
{
use \MEkramy\OOPUtil\CanChained;
protected $calls = [];
public function __call($name, $arguments)
{
$this->calls[] = $name;
}
public function __cantChain(){
return ['print'];
}
public function print(){
print_r($this->calls);
}
}
$dummy = new Dummy();
$dummy->chaining()->A()->B()->C()->D();
$dummy->print(); // Print: ['A', 'B', 'C', 'D']
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.