Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
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
4 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
4 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
4 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
4 years ago
0
PHP
OOP
PHP Object Oriented Programming (OOP)
2
Returns false if the email is not in a correct format (if the email is not valid)
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $this->addError('email', 'email must be a valid email');
}
by Valeri Tandilashvili
4 years ago
0
PHP
functions
Object Oriented PHP Tutorial
2
Value
Specifies how much of the task has been completed
Max
Specifies how much work the task requires in total
Status: <progress min="0" max="100" value="35"> 
</progress>
by Valeri Tandilashvili
3 years ago
0
HTML
attributes
2
Triggers error if
$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
by Valeri Tandilashvili
4 years ago
0
PHP
functions
Object Oriented PHP Tutorial
2
Checks if the specified key is in the array
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
by Valeri Tandilashvili
4 years ago
0
PHP
functions
2
Two classes extends
Animal
abstract class and both implement the same method
prey()
differently
abstract 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!
by Valeri Tandilashvili
4 years ago
0
PHP
OOP
2
If we have a complex object, and we’d like to convert it into a string, to send it over a network, or just to output it for logging purposes, we can use
toString
method
let 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}
by Valeri Tandilashvili
3 years ago
0
JavaScript
objects
JavaScript tutorial
2
Results: 1578