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.该版本可以作为我日后的回忆,哈哈。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool