Last active
August 31, 2021 08:15
-
-
Save AbmSourav/7b28333a7585043beb92e14ff90b021b to your computer and use it in GitHub Desktop.
A simplified way of Laravel's Facade...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Facade { | |
abstract protected static function getInstance(); | |
public static function __callStatic($method, $arguments) { | |
$instance = static::getInstance(); | |
if (! $instance) { | |
throw new Exception('Method not found'); | |
} | |
return $instance->$method(...$arguments); | |
} | |
} | |
// Example: | |
class Test extends Facade { | |
private static $self; | |
protected static function getInstance() { | |
if (static::$self === null) { | |
static::$self = new Test; | |
} | |
return static::$self; | |
} | |
protected function check_validation($data) { | |
return 'Data is valid...'; | |
} | |
} | |
Test::check_validation('Hi'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment