Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active May 27, 2019 00:01
Show Gist options
  • Save harisrozak/e015511b2f1a3defff608f56518852ad to your computer and use it in GitHub Desktop.
Save harisrozak/e015511b2f1a3defff608f56518852ad to your computer and use it in GitHub Desktop.
PHP :: Learn Namespace
<?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();
<?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();
}
<?php
namespace Haris\Learn\OONamespace\ModuleThree;
Class Fuel {
private static $volume = 0;
static function status() {
printf("<br>&nbsp; fuel volume: %s", self::$volume);
}
static function fill() {
self::$volume = 100;
}
}
Class Oil {
private static $volume = 0;
static function status() {
printf("<br>&nbsp; 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();
}
<?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>&nbsp; engine version: %s", $this->engine_version);
printf("<br>&nbsp; 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