function route_request() { $cmd = strtolower(grab_request_var("cmd")); // token if required for most everyting if ($cmd != "" && $cmd != "hello") { check_token(); } //echo "CMD='$cmd'<BR>"; switch ($cmd) { // say hello case "hello": say_hello(); break; // display a form for debugging/testing // display a form for debugging/testing case "": display_form(); break; default: //echo "PASSING TO PLUGINS<BR>"; // let plugins handle the output $args = array("cmd" => $cmd); do_callbacks(CALLBACK_PROCESS_REQUEST, $args); break; } echo "NO REQUEST HANDLER"; exit; }
/** * ------------------------------------------------------------------- * LtAutoloader要求: * # 需要被自动加载的文件都以.php或者.inc结尾 * 如果既有php文件,又有html文件,html文件将被忽略,php文件正常加载 * 可配置,详情参见LtAutoloaderCofig * * ------------------------------------------------------------------- * LtAutoloader不在意: * # 目录名有没有拖尾斜线 * # 目录下面有无子目录 * # 文件名和文件路径跟类名有无关联 * # 定义和使用类时,类名是大写还是小写 * * ------------------------------------------------------------------- * LtAutoloader不支持(出错演示和不支持的原因参见WrongWayToUse.php): * # 传入的参数不是真实存在的目录(如http://some_dir这样的) * # 目录名或者文件名带空格(如"Zend Framework"这样的目录名) * # 类或接口重名,函数和函数重名 * * ------------------------------------------------------------------- * LtAutoloader建议(不强求): * # 传入autoloadPath时使用绝对路径 * # 目录名和文件名只由字母、数字、下划线、中横线、小数点组成 * # 使用class而不是function来封装你的逻辑 * # 每个class都放在单独的一个文件中,且不要在已经定义了类的文件里再定义函数 * # class/function里不要使用__FILE__魔术变量 * * ------------------------------------------------------------------- * 本测试用例期望效果: * 在new CLASS_NAME, class_exists("CLASS_NAME"), extends CLASS_NAME的时候 * 自动把包含该类的文件加载进来 */ public function testMostUsedWay() { /** * Lotus组件初始化三步曲 */ // 1. 实例化 $autoloader = new LtAutoloader(); // 2. 设置属性 $autoloader->autoloadPath = array(dirname(__FILE__) . "/test_data/class_dir_1", dirname(__FILE__) . "/test_data/class_dir_2", dirname(__FILE__) . "/test_data/function_dir_1", dirname(__FILE__) . "/test_data/function_dir_2"); // 3. 调init()方法 $autoloader->init(); //初始化完毕,测试其效果 $this->assertTrue(new Goodbye() instanceof GoodBye); $this->assertTrue(class_exists("HelloWorld")); $this->assertEquals("hello", HelloLotus::sayHello()); $this->assertEquals("hello", say_hello()); $this->assertEquals("hello_2", say_hello_2()); }
<html> <body> <?php //include 'file.php?foo=1&bar=2'; //include 'http://www.example.com/file.php?foo=1&bar=2'; echo "A {$color} {$fruit}" . '<br>'; // A include 'simple_inclusion.php'; echo "A {$color} {$fruit}" . '<br>'; // A green apple say_hello(); if ((include 'simple_inclusion.php') == 'OK') { echo 'Fail'; } ?> </body> </html>
<?php function say_hello($some_name) { echo "Hello," . $some_name . "! <br/>"; echo "<br/>"; } say_hello("Jenny"); //这里使用参数“Jenny”调用函数say_hello say_hello("Harry"); say_hello("Ema");
<?php function say_hello($name = 'Buddy') { //default is 'Buddy' return 'Hi, there ' . $name; } function pp($value) { echo "<pre>"; print_r($value); echo "</pre>"; } function array_pluck($to_pluck, $arr) { $new_array = array(); foreach ($arr as $item) { $new_array[] = $item['age']; } } echo say_hello('Joe'); echo '<br>'; $arr = array('name' => 'Joe', 'age' => 40, 'occupation' => 'teacher'); echo "<pre>"; print_r($arr); echo "</pre>"; pp($arr); $people = array(array('name' => 'Joe', 'age' => 40, 'occupation' => 'teacher'), array('name' => 'Matt', 'age' => 34, 'occupation' => 'doctor'), array('name' => 'Dude', 'age' => 56, 'occupation' => 'janitor')); array_pluck('name', $people); //
while ($age = current($ages)) { echo $age . ", "; next($ages); } */ /************************************************************ * FUNCTIONS ************************************************************/ // You can call first, and define later, but can't redefine it function say_hello($word) { // Define a function echo "Hello {$word}!" . "<br />"; } $the_word = "World"; say_hello($the_word); // Call the function // More Arguments function better_hello($greeting, $target, $punct) { echo $greeting . " " . $target . $punct . "<br />"; } $name = "John Doe"; better_hello("Greetings", $name, "!"); better_hello("Greetings", $name, null); // Must have arguments, even "null" // Return value function add($val01, $val02) { $sum = $val01 + $val02; return $sum;
function test1() { $buf = say_hello('Mr', 'Daniel', 'Day' . ' ' . get_last_name()); echo "Test a few basic things. :-)<br>\n"; echo 'result: ' . ($buf === 'hello Mr Daniel Day Libby!' ? 'pass' : 'fail') . "<br><br>\n\n"; }
<?php function say_hello() { return "Hi, there"; } say_hello(); //Nothing show echo say_hello(); // Display Hi, there /* * Passing parameter * */ function hello($name) { return "Hello, {$name}"; } echo hello("Biswajit"); function array_pluck($toPluck, $arr) { $ret = array(); foreach ($arr as $item) { $ret[] = $item[$toPluck]; } return $ret; } $people = array(array('name' => 'Biswajit', 'age' => '27', 'occupation' => 'student'), array('name' => 'Jeffery', 'age' => '50', 'occupation' => 'Web Developer'), array('name' => 'Joe', 'age' => '50', 'occupation' => 'Marketing')); print_r(array_pluck('name', $people));
<?php function say_hello($name) { return "Hello, {$name}"; } echo say_hello('Ayoze Vera'); echo say_hello('Pepe Vera');
<?php $name = "Brian"; function say_hello($name = "default") { //default is overrided by "Brian" as provided by $name return "Hello, {$name}!<br/>"; } echo say_hello($name); $arr = array("name" => "Brian", "age" => "16", "occupation" => "student"); function pp($value) { echo "<pre>"; print_r($value); echo "</pre>"; } function array_pluck($toPluck, $arr) { return array_map(function ($item) use($toPluck) { return $item[$toPluck]; }, $arr); } pp($arr); $people = array(array("name" => "Brian", "age" => "16", "occupation" => "student"), array("name" => "Joe", "age" => "50", "occupation" => "teacher"), array("name" => "Jane", "age" => "30", "occupation" => "marketing")); $plucked = array_pluck("name", $people); print_r($plucked);
# ARRAY_PLUCK() and ARRAY_MAP() // Goes with EXAMPLE #2 and EXAMPLE #3 require 'functions.php'; /* FUNCTIONS: ARE REUSABLE bits of code. Rather than writing functions manually many times, you can ABSTRACT that code away * to a FUNCTION that you reference. If you need to change it, you can now CHANGE IT ONCE in your project. */ // EXAMPLE #1: // SYNTAX: function name() {do something...} // Make functions short, to the point, and accomplishes only one thing // if you SET the PARAMETER equal to something it will be the DEFAULT ARGUMENT ex) 'buddy' function say_hello($name_ex = 'buddy') { // $name is the value we would accept from the say_hello function (PARAMETER) return "Hi, there {$name_ex} <br>"; } $greeting = say_hello(); // We can pass in a value to the say_hello function (ARGUMENT) echo $greeting; // EXAMPLE #2: // Real world example: We don't want to echo a <pre></pre> EVERY TIME we need to do the below (in comments) $arr = array('name' => 'Joe', 'age' => 40, 'occupation' => 'teacher'); // echo '<pre>'; // Recognizes code formatting // print_r($arr); // echo '</pre>'; pretty_print($arr); // Call the function (in functions.php) and pass in the $arr // EXAMPLE #3 // MULTI-DIMENSIONAL ARRAY that gets the names "plucked" from them $people = array(array('name' => 'Lucas', 'age' => 29, 'occupation' => 'software developer'), array('name' => 'Joe', 'age' => 50, 'occupation' => 'teacher'), array('name' => 'Jane', 'age' => 30, 'occupation' => 'marketting')); // If we change the 1st ARGUMENT, we can get any one of the KEYS (name, age, or occupation) from the Multi. Dimens. Array $plucked = array_pluck('name', $people);
<?php function say_hello($name) { return 'hello, ' . $name; } $greeting = say_hello('John Doe'); echo $greeting;