Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Last active October 4, 2015 02:07
Show Gist options
  • Save hidayat365/2559703 to your computer and use it in GitHub Desktop.
Save hidayat365/2559703 to your computer and use it in GitHub Desktop.
Hello World OOP
<?php
/*
* contoh interface
*/
interface Printer
{
public function printOut($message);
}
/*
* class Message
* berisi pesan string yang ingin ditampilkan
*/
class Message
{
private $message;
public function __construct($message) {
$this->message = $message;
}
public function printOut($printer) {
$printer->printOut($this->toString());
}
public function toString() {
return $this->message;
}
}
/*
* contoh class abstract
* harus dibuat object inheritance kalau mau pake
*/
abstract class AbstractPrinterFactory
{
public static function getFactory(){
return new SystemOutPrinterFactory();
}
public abstract function getPrinter();
}
/*
* contoh inheritance dari class abstract
*/
class SystemOutPrinterFactory extends AbstractPrinterFactory
{
public function getPrinter() {
return new SystemOutPrinter();
}
}
/*
* contoh implementasi interface
*/
class SystemOutPrinter implements Printer
{
public function printOut($message) {
print $message;
}
}
/*
* program utama :)
*/
class HelloWorld
{
public static function run($arg)
{
$message = new Message($arg);
$factory = AbstractPrinterFactory::getFactory();
$printer = $factory->getPrinter();
$message->printOut($printer);
}
}
/*
* Sekarang jalankan aplikasi
*/
HelloWorld::run("Hello, World!");
@hiraq
Copy link

hiraq commented Oct 21, 2013

the most amazing "hello world" syntax :o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment