Skip to content

Instantly share code, notes, and snippets.

@bosz
Created March 2, 2021 13:24
Show Gist options
  • Save bosz/f75ad816c6c2d57ff28a8cd6d1acb84a to your computer and use it in GitHub Desktop.
Save bosz/f75ad816c6c2d57ff28a8cd6d1acb84a to your computer and use it in GitHub Desktop.
Revising php (Interace, &, Abstract, Traits and Facade
<?php
/**
* Interface (check)
* Abstract (check)
* Trait (check)
* & (check)
* Facade (check)
*/
interface Interace1 {}
interface Interace2 {}
interface Interace3 {}
interface IAnimal extends Interace1, Interace2, Interace3{
public function run();
}
class MyClass {}
abstract class AAnimal extends MyClass {
abstract public function eat();
public function prey() {
echo " I catch prey";
}
public function start() {
echo "I am starting from abstract<br>";
}
}
trait MyUseClass {
public function start() {
// parent::start();
echo "I am starting from use <br>";
}
public static function stop() {
echo "I am stopping";
}
}
class Animal extends AAnimal implements IAnimal{
use MyUseClass;
public function eat() {
echo "This is how i eat";
}
public function run() {
echo "This is how i run";
}
}
$animal = new Animal;
$animal->start();
echo "<hr><hr>";
$ar = [2,4];
var_dump($ar);
foreach($ar as &$val) {
$val *= 1;
}
echo "<br>";
var_dump($ar);
echo "<hr><hr>";
$a = 1;
$b = 2;
echo $a . $b;
$a = &$b;
$b = 8;
echo $a . $b;
echo "<hr>By reference<hr>";
class foo {
public $value = 42;
public function &getValue() {
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$myValue = 1203;
echo $obj->value;
echo "<br>";
$obj->value = 2;
echo $myValue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment