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
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
interfaceclass 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();
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();
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->addError('email', 'email must be a valid email');
}
Value
Specifies how much of the task has been completed
Max
Specifies how much work the task requires in totalStatus: <progress min="0" max="100" value="35">
</progress>
$divisor
equals to 0$divisor = false;
if ($divisor == 0) {
trigger_error("Cannot divide by zero", E_USER_WARNING);
}
Possible second parameter values:
E_USER_NOTICE
E_USER_WARNING
E_USER_ERROR
The second parameter defaults to E_USER_NOTICE$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
Animal
abstract class and both implement the same method prey()
differentlyabstract class Animal {
// child classes must implement this
abstract function prey();
public function run() {
echo "I am running!\n";
}
}
class Dog extends Animal {
public function prey() {
echo "I killed the cat !\n";
}
}
class Cat extends Animal {
public function prey() {
echo "I killed the rat !\n";
}
}
$dog = new Dog();
$cat = new Cat();
$dog->prey(); // I killed the cat !
$cat->prey(); // I killed the rat !
$dog->run(); // I am running!
$cat->run(); // I am running!
toString
methodlet user = {
name: "John",
age: 30,
toString() {
return `{name1: "${this.name}", age1: ${this.age}}`;
}
};
document.write(user);
// document.write(JSON.stringify(user));
The code above will produce the following result:{name1: "John", age1: 30}