《PHP编程:PHP新建类问题分析及解决思路》要点:
本文介绍了PHP编程:PHP新建类问题分析及解决思路,希望对您有用。如果有疑问,可以联系我们。
PHP应用下面先给大家分析php新建类的问题
PHP应用index.php文件
- PHP应用
- function __autoload($_className) {
- require $_className.'.class.php';
- }
- //新建类??
- if (isset($_GET['index'])) {
- $m=new Main($_GET['index']);
- }else{
- $m=new Main();
- }
- include $m->ui();
PHP应用main.class.php文件
- PHP应用
- class Main{
- private $index;
- //构造办法,初始化数据
- public function __construct($index=''){
- $this->index=$index;
- }
- //ui函数include相应的包含文件
- public function ui(){
- if(empty($this->index)||!file_exists($this->index.'.inc')){
- $this->index='start';
- }
- return $this->index.'.inc';
- }
- }
PHP应用红字的部分有啥意义了:类中构造函数传参值已设默认是空(public function __construct($index='')),为啥不能直接写$m=new Main($_GET['index']);.如果不想在index做红字的if判断,类里需要怎么写了.谢谢,不是太理解
PHP应用------办理思路----------------------
- PHP应用
- if (isset($_GET['index'])) {
- $m=new Main($_GET['index']); //如果 $_GET['index'] 存在则将 $_GET['index'] 作为参数
- }else{
- $m=new Main(); //不然使用默认参数
- }
PHP应用直接使用 $_GET['index'] 将可能引发 NOTICE 级别错误
PHP应用不加区别的使用传入数据,可能引发平安问题
PHP应用------办理思路----------------------
PHP应用稍微改了一下你看咋样.
- PHP应用
- <?php
- class Main{
- private $index;
- //构造办法,初始化数据
- public function __construct($index='')
- {
- $this->index=$index?$index:'';
- }
- //ui函数include相应的包含文件
- public function ui()
- {
- if(empty($this->index)
PHP应用------解决思路----------------------
- PHP应用
- !file_exists($this->index.'.inc'))
- {
- $this->index='start';
- }
- return $this->index.'.inc';
- }
- }
PHP应用ps:php怎么创建文件?
PHP应用php项目开发过程中,常常必要自动创建一些文件,如生成静态html,生成php缓存文件,生成txt文件等等.下面就分享一下如何利用php程序创建文件,并向文件中写入内容.
PHP应用一个项目中,可能不止一次必要生成文件,因此我们可以定义一个函数,当必要创建文件时再来调用这个函数,即可.
PHP应用步调一、定义函数writefile,用于以写的方式打开一个文件,文件不存在时自动创建,并向文件写入内容,代码如下.
- PHP应用
- <?php
- function writefile($fname,$str){
- $fp=fopen($fname,"w");
- fputs($fp,$str);
- fclose($fp);
- }
- ?>
PHP应用步调二、函数的使用.如创建test.txt文件,并写入内容“abc”,代码如下:
- PHP应用
- <?php
- $filename='test.txt';
- $str='abc';
- writefile($filename,$str);
- ?>
PHP应用通过上述两个步调的操作,即可实现php创建文件的功能.
维易PHP培训学院每天发布《PHP编程:PHP新建类问题分析及解决思路》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/8371.html