您的位置:首页 > 编程语言 > PHP开发

认真学习php面向对象-3

2017-08-07 11:57 387 查看

认真学习php面向对象-3

前言

准备写一个认真学习php面向对象的系列,使用php来做网页,没有深入了解php的话,可能三板斧就够了,并不需要有多高深!如有错误,欢迎各位不吝赐教!进度安排的话,我学到哪里,就更新到哪里了!形式的话就采用一个需求小案例,然后实现,并附上自己的总结,文章源码

所用到的环境

系统:ubuntu16.04

编辑器:phpstorm2017

php7

需求 :1)使用自定义模板创建入口文件

解决 : ob_start以及get_object_vars和extract的使用

ob_start

此函数将打开输出缓冲。当输出缓冲激活后,脚本将不会输出内容(除http标头外),相反需要输出的内容被存储在内部缓冲区中

我们可以使用此函数加载自定义的模板文件,但并没有输出到屏幕中,而是用变量获取数据,并将数据写入到创建的入口文件当中

get_object_vars

返回由对象属性组成的关联数组

可以获取该对象的属性值,并创建用户输入的参数文件目录以及写入用户的数据

extract

从数组中将变量导入到当前的符号表,可以将数组直接转换成变量(key)=值(value)

实现 :

创建自定义模板文件(template文件下创建index.tpl)

index.tpl

<?php echo '<?php' ?>
/**
* project name <?php echo $prj_name?>
* user <?php echo $author?>
* date <?php echo date('Y-m-d')?>
*/


写入模板文件所需要的变量值

god_class.hp

static function start() {
$config=loadconfig();
$ff=new god_frame($config->prj_name);
$ff->prj_name=$config->prj_name;
$ff->author=$config->author;
$ff->run();
}


在创建文件目录类对输入的参数处理以及生成模板对应自定义文件

god_frame.hp

function run() {
!file_exists($this->project_folder) && mkdir($this->project_folder);
!file_exists($this->project_main) && file_put_contents($this->project_main,'');

extract(get_object_vars($this));
ob_start();
include (dirname(__FILE__).'/template/index.tpl');
$cnt=ob_get_contents();
ob_end_clean();
file_put_contents($this->project_main,$cnt);

}


效果 :



需求 :2)使用php内置服务器部署网站,供开发测试使用

解决 : system和php命令行启动内置服务器命令

system

同 C 版本的 system() 函数一样, 本函数执行 command 参数所指定的命令, 并且输出执行结果。

启动php内置服务器

php启动内置服务器方法

php -S “可以随意设置访问的地址” -t 项目的目录地址

如:php -S localhost:8080 -t /home/shisiying/code/phpOrinentedObject/app

实现 :

使用system函数进行执行

god_frame.hp

function run() {
!file_exists($this->project_folder) && mkdir($this->project_folder);
!file_exists($this->project_main) && file_put_contents($this->project_main,'');

extract(get_object_vars($this));
ob_start();
include (dirname(__FILE__).'/template/index.tpl');
$cnt=ob_get_contents();
ob_end_clean();
file_put_contents($this->project_main,$cnt);

echo "the server is running!";
system("php -S localhost:8080 -t /home/shisiying/code/phpOrinentedObject/app");

}


效果 :



其中it works在生成的后续添加的,方便观察效果



需求 :3)对只写变量的文件模拟编译

解决 : scandir和get_defined_vars()和var_export的使用
4000

scandir

列出指定路径中的文件和目录

get_defined_vars

返回由所有已定义变量所组成的数组

var_export

输出或返回一个变量的字符串表示,函数的第二个参数设置为 TRUE,从而返回变量的表示。

实现 :

创建一个文件



**

god_frame.php

function compile() {
$_files=scandir($this->project_folder.'/code');
foreach ($_files as $_file) {
if (preg_match("/\w+\.var\.php$/i",$_file)) {
require ($this->project_folder.'/code/'.$_file);
unset($_file);
}
}
unset($_files);
$result='<?php'.PHP_EOL
.'extract('.var_export(get_defined_vars(),1).');';
file_put_contents($this->project_folder.'/vars',$result);
}


god_class.php

//模拟编译
static function compile() {
$config=loadconfig();
$gf=new god_frame($config->prj_name);
$gf->compile();
}


效果 :



需求 :4)对只写函数的文件模拟编译

解决 : scandir和get_defined_function()和ReflectionFunction的使用

scandir

列出指定路径中的文件和目录

get_defined_vars

返回由所有已定义变量所组成的数组

var_export

输出或返回一个变量的字符串表示,函数的第二个参数设置为 TRUE,从而返回变量的表示。

get_defined_function()

返回所有已定义函数的数组

ReflectionFunction

报告了一个函数的有关信息。使用方法如下

array_slice

从数组中取出一段,数组的截取函数

implode

将一个一维数组的值转化为字符串

unset

释放给定的变量

实现 :

创建一个函数文件



god_frame.php

function compile() {
$_files=scandir($this->project_folder.'/code');
foreach ($_files as $_file) {
if (preg_match("/\w+\.(var|func)\.php$/i",$_file)) {
require ($this->project_folder.'/code/'.$_file);
unset($_file);
}
}
unset($_files);
$result='<?php'.PHP_EOL
.'extract('.var_export(get_defined_vars(),1).');';
file_put_contents($this->project_folder.'/vars',$result);

$funs=get_defined_functions();
foreach ($funs['user'] as $fun) {
$f=new \ReflectionFunction($fun);
$start=$f->getStartLine();
$end=$f->getEndLine();
$fileList=file($f->getFileName());
$re = '<?php'.PHP_EOL
.implode(array_slice($fileList,$start-1,$end-$start+1));
file_put_contents($this->project_folder.'/func',$re);
}
}


效果 :



需求 :5)模拟简单的mvc模式,接收浏览器访问的mv并进行路由简单处理

解决 : $_SERVER[‘PATH_INFO’]和explode的使用

实现 :

创建一个class文件



index.php

$pi=$_SERVER['PATH_INFO'];
$controller=explode('/',$pi)[1];
$method=explode('/',$pi)[2];
require (getcwd().'/code/'.$controller.".class.php");
$get_class=new $controller();
$get_class->$method();


效果 :

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 面向对象