Skip to content

Instantly share code, notes, and snippets.

@uzulla
Created March 9, 2014 08:57
Show Gist options
  • Save uzulla/9444783 to your computer and use it in GitHub Desktop.
Save uzulla/9444783 to your computer and use it in GitHub Desktop.
「継承できるテンプレートとは」(ネタです)
<?php
class Mother {
static $html = '
<html>
<body>
{{content}}
</body>
</html>
';
public function __construct($my_name=null, $var=null){
$class_name = (is_null($my_name)) ? get_called_class() : $my_name;
$class_name::$html = (!is_null($var)) ? preg_replace('/{{content}}/', $var, $class_name::$html) : $class_name::$html;
$parent_list = array_keys(class_parents($class_name));
if(!empty($parent_list))
$parent_list[0]::__construct($parent_list[0], $class_name::$html);
else
echo $class_name::$html;
}
}
class Child extends Mother {
static $html = 'Hello {{content}}';
}
class GrandChild extends Child {
static $html = 'world!!';
}
// Execute
new GrandChild();
@uzulla
Copy link
Author

uzulla commented Mar 9, 2014

$ php funny.php

    <html>
    <body>
    Hello world!!
    </body>
    </html>

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