2014年辛星starphp第一节设置入口文件以及App类
*********************本节目标****************
1.首先是我们的框架大致布局,我们即将写成的这个框架,它的入口文件统一为star.php,它需要做的一些事,比如加载配置项,日志文件什么的日后再说,首先确定一下它的目录结构,它的目录结构是如下的样子:
|---------star| |------------core:核心类库| | |---------------app.php| | |---------------model.php| | |---------------controller.php| | |----------------view.php| | |-----------------fun.php| || |-------------common:函数库| |-------------class:类库| |-------------extends:其他类库| |--------------star.php||-----------app它的文件目录为| |------------遵循模块/控制器/方法的格式| |------------index.php||------------adimin|2.我们今天先写这个star.php文件,它是统一的入口文件,首先我们必须定义一个index.php,它的文件内容如下:
<?php //定义它是从首页进入的define('INDEX',True);//包含该核心文件define('__ROOT__',__DIR__);include __ROOT__.'/star/star.php';
3.然后我们书写star.php的内容,它能够引导找到App类,并且调用App类的run方法来使程序运行下去,它的代码:
<?php //定义版本信息define("VERSION","0.1");//控制PHP版本if(phpversion() <'5.3'){ exit("版本太低不支持");} //表示路径分隔符define("DS",DIRECTORY_SEPARATOR);//这个STAR表示我们的star目录if(!defined("STAR")){define("STAR",__DIR__);}//定义应用程序目录,if(! defined("APP")){define("APP",__ROOT__.DS."app");}if(! defined("CORE")){define("CORE",STAR.DS."core");}//导入应用程序控制文件 include STAR.DS."core".DS."app.php";//导入核心文件include CORE.DS."fun.php";$app = new App();$app->run();4.然后我们写这个App类,它应该能够完成对URL的解析,这里我们还没有设置配置文件,因此我们的URL统一使用PATHINFO模式,而且路径分隔符统一用斜线,而且我们的url的文件名的后部分是按照”模块/控制器/方法/参数1/值1/参数2/值2...."的方式去写的,因此,我们的app.php文件如下:
<?php /***该类用于统一处理所有的信息**/if(!defined("STAR")) die("系统错误");include STAR.DS."core".DS."controller.php";class App{ //模块名 private $module = ""; //控制器 private $controller = ""; //方法名 private $method = ""; //参数名 private $param = array(); //参数个数 private $paramlength = 0; /** *用于解析控制器和方法 * */ public function __construct(){ //默认使用/来解析url $path = trim($_SERVER['PATH_INFO'],'/'); $path = explode('/',$path); var_dump($path); $paramlength =(count($path) - 3)/2; var_dump($paramlength); $this->paramlength = $paramlength; $module = array_shift($path);//模块名 $controller = array_shift($path);//控制器名 $method = array_shift($path);//方法名 var_dump($path); for($i = 0;$i param = $param; if($module ==""){$module = "index";} if($controller == ""){$controller = "Index";} if($method == ""){$method = "index";} $this->module = $module; $this->controller = $controller; $this->method = $method; //spl_autoload_register($this->loadcore); //自动根据解析的路由来执行 } /** *用于运行方法 * */ public function run(){ $controller = $this->controller; $module = $this->module; $dir = APP.DS.$module.DS."controller".DS."$controller"."Controller.php"; include "$dir"; $controllerclass = $controller."Controller"; $class = new $controllerclass(); $method = $this->method; $param = $this->param; $length = $this->paramlength; if(is_int($length) && ($length >= 1)){ $class->$method($param); }else{ $class->$method(); } } /** * * */ }5.我们还需要在core文件夹的fun.php文件中添加一些代码,它有一个star_arr_safe函数用来对数组进行过滤,来防止非法注入,它的内容如下:
<?php /***用于过滤用户输入信息的函数*它主要是防止sql注入*也需要防范html实体*/function star_arr_safe($array){ if(is_array($array)){ $count = count($array); for($i = 0;$i< $count;$i ++){ $array[$i] = htmlspecialchars($array[$i]); $array[$i] = addslashes($array[$i]); } } return $array; }
6.我们的Controller还没有实际的意义,但是为了能够起到形式上的作用,我们定义如下:
<?php /***该类用于解析url并且根据url来执行相关的方法**/if(!defined("STAR")) die("系统错误");class Controller{ }7.截至目前,我们先写这些代码。
*****************辅助工作***************
1.为了测试运行,我们在app目录下的controller目录下新建了一个MyController.php,它有一个index方法,我们这里给定如下:
<?phpif (!defined("STAR")) exit("Not Allowed");class MyController extends Controller{ public function index($arr){ echo "hello world"; var_dump($arr); }}2.该版本目前运行正常,版本号记为0.0.1,代码我上传到csdn的下载的地方:
点击打开链接 ,当然是面积分下载的。
3.该版本可以作为我日后的回忆,哈哈。

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
