Last active
May 27, 2019 00:01
-
-
Save harisrozak/e015511b2f1a3defff608f56518852ad to your computer and use it in GitHub Desktop.
PHP :: Learn Namespace
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 | |
// Main namespace | |
namespace Haris\Learn\OONamespace; | |
// Constant | |
const FILE = __FILE__; | |
// Require files | |
require_once dirname(FILE) . '/module-one.php'; | |
require_once dirname(FILE) . '/module-two.php'; | |
require_once dirname(FILE) . '/module-three.php'; | |
// Init | |
ModuleOne\init(); |
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 | |
namespace Haris\Learn\OONamespace\ModuleOne; | |
use Haris\Learn\OONamespace\ModuleTwo; | |
use Haris\Learn\OONamespace\ModuleThree; | |
function init() { | |
echo "initialize machine.."; | |
$boot = ModuleTwo\Boot::getInstance(); | |
$boot->engine_check(); | |
$boot->fuel_oil_check(); | |
// fill fuel and oil | |
ModuleThree\fill_fuel_and_oil(); | |
} |
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 | |
namespace Haris\Learn\OONamespace\ModuleThree; | |
Class Fuel { | |
private static $volume = 0; | |
static function status() { | |
printf("<br> fuel volume: %s", self::$volume); | |
} | |
static function fill() { | |
self::$volume = 100; | |
} | |
} | |
Class Oil { | |
private static $volume = 0; | |
static function status() { | |
printf("<br> oil volume: %s", self::$volume); | |
} | |
static function fill() { | |
self::$volume = 80; | |
} | |
} | |
// do fill fuel and oil | |
function fill_fuel_and_oil() { | |
printf("<br>%s", 'filling the fuel and oil..'); | |
// filling | |
Fuel::fill(); | |
Oil::fill(); | |
// echo status | |
Fuel::status(); | |
Oil::status(); | |
} |
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 | |
namespace Haris\Learn\OONamespace\ModuleTwo; | |
use Haris\Learn\OONamespace\ModuleThree; | |
Class Boot { | |
// instance | |
private static $instance; | |
// variable | |
private $engine_version = '2.0'; | |
private $engine_cc = '2500'; | |
public static function getInstance() { | |
if(! isset(self::$instance)) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
public function engine_check() { | |
printf("<br>%s", 'checking engine..'); | |
$this->display_status(); | |
} | |
public function fuel_oil_check() { | |
printf("<br>%s", 'checking fuel and oil..'); | |
ModuleThree\Fuel::status(); | |
ModuleThree\Oil::status(); | |
} | |
private function display_status() { | |
printf("<br> engine version: %s", $this->engine_version); | |
printf("<br> engine cc: %s", $this->engine_cc); | |
} | |
private function __construct() { | |
/* private for singleton purpose */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment