×
Clear all filters including search bar
Valeri Tandilashvili's Personal Professional Blog
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();
$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_NOTICEprint_r(json_encode(getallheaders()));
The result of the above code is:{
"Content-Type": "application/json",
"User-Agent": "PostmanRuntime/7.26.8",
"Accept": "*/*",
"Postman-Token": "a89b677d-776a-45d7-988c-f18ad2512522",
"Host": "localhost",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Content-Length": "137"
}
http_response_code
Sets HTTP response status code 404
http_response_code(404);
Late Static Binding
gives us ability to call child class method from parent class.
In this example who()
method of child class will be called from parent class test()
methodclass A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
Another example of late static binding: parent class uses child class property - tableName
class Model
{
protected static $tableName = 'Model';
public static function getTableName()
{
return static::$tableName;
}
}
class User extends Model
{
protected static $tableName = 'User';
}
echo User::getTableName(); // User
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);
<button type="button" class="btn btn-outline-primary">Primary</button>
<button type="button" class="btn btn-outline-secondary">Secondary</button>
<button type="button" class="btn btn-outline-success">Success</button>
<button type="button" class="btn btn-outline-danger">Danger</button>
<button type="button" class="btn btn-outline-warning">Warning</button>
<button type="button" class="btn btn-outline-info">Info</button>
<button type="button" class="btn btn-outline-light">Light</button>
<button type="button" class="btn btn-outline-dark">Dark</button>
Outline button Classes:
btn-outline-primary
btn-outline-secondary
btn-outline-success
btn-outline-danger
btn-outline-warning
btn-outline-info
btn-outline-light
btn-outline-dark
.clearfix::after {
content: "";
clear: both;
display: block;
}
Modern way of clearing floats