クラスを必要なときに読み込むための仕組み
function __autoload($name)
{
$file_path = 'lib/' . $name . '.php';
is_readable($file_path) && require $file_path;
}
$some_lib = new SomeLib();
function example1_add($v1, $v2)
{
return $v1 + $v2;
}
Class Math
{
public function example1_sub($v1, $v2)
{
return $v1 - $v2;
}
public static function example1_add($v1, $v2)
{
return $v1 + $v2;
}
}
call_user_func('example1_add', 1, 2);
call_user_func(function($v1, $v2) { return $v1 + $v2; }, 1, 2);
call_user_func(array('Math', 'example1_add'), 1, 2);
call_user_func('Math::example1_add', 1, 3);
$math = new Math();
call_user_func(array($math, 'example1_sub'), 1, 3);
call_user_func_array('example1_add', array(1, 2));
call_user_func_array(array($math, 'example1_sub'), array(1, 2));
[PHP] 続・PHPテンプレートエンジンを10行で自作する
Webフォルダ
|
+-- class/
| |
| +-- MyTemplate.class.php (1)
|
+-- template/
| |
| +-- sample.tpl.html (2)
|
+-- sample.php (3)
<?php
class MyTemplate
{
function show($tpl_file)
{
$v = $this;
include(__DIR__ . "/../template/{$tpl_file}");
}
}
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>店名:<?php echo $v->name; ?></h1>
<ul>
<?php for($i=0; $i<count($v->foods); $i++){ ?>
<li><?php echo $v->foods[$i]; ?></li>
<?php } ?>
</ul>
</body>
</html>
<?php
include(__DIR__ . "/class/MyTemplate.class.php");
$tpl = new MyTemplate();
$tpl->name = '悟空軒';
$tpl->foods = array('ラーメン', '餃子', '焼き飯', '麻婆豆腐');
$tpl->show('sample.tpl.html');
?>