PHP new class problem analysis and solution ideas, PHP new class ideas_PHP tutorial

WBOY
Release: 2016-07-12 09:04:33
Original
964 people have browsed it

PHP new class problem analysis and solution ideas, php new class ideas

The following will first analyze the problem of php new class

index.php file

function __autoload($_className) {
require $_className.'.class.php';
}
<span style="color: #ff0000">//新建类??
if (isset($_GET['index'])) {
$m=new Main($_GET['index']);
}else{
$m=new Main();
</span>}
include $m->ui();
Copy after login

main.class.php file

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';
} 
}
Copy after login

What’s the meaning of the part in red?: The parameter value passed by the constructor in the class has been set to be empty by default (public function __construct($index='')), why can’t we write $m= directly? new Main($_GET['index']);. If you don’t want to make a red-letter if judgment in the index, what should you write in the class? Thank you, I don’t quite understand

------Solution Idea----------------------

if (isset($_GET['index'])) {
 $m=new Main($_GET['index']); //如果 $_GET['index'] 存在则将 $_GET['index'] 作为参数
}else{
 $m=new Main(); //否则使用默认参数
}
Copy after login

Using $_GET['index'] directly may cause a NOTICE level error

Indiscriminate use of incoming data may cause security issues

------Solution Idea----------------------

What do you think after slightly changing it?

<&#63;php
class Main{
 private $index;
 //构造方法,初始化数据
 public function __construct($index='')
 {
 $this->index=$index&#63;$index:'';
 }
 //ui函数include相应的包含文件
 public function ui()
 {
 if(empty($this->index)
Copy after login

------Solution Idea----------------------

!file_exists($this->index.'.inc'))
 { 
 $this->index='start';
 }
 return $this->index.'.inc';
 } 
}
Copy after login

ps: How to create a file in php?

During the development process of PHP projects, it is often necessary to automatically create some files, such as generating static HTML, generating PHP cache files, generating txt files, etc. Let's share how to use the php program to create files and write content to the files.

In a project, files may need to be generated more than once, so we can define a function and call this function when a file needs to be created.

Step 1. Define the function writefile, which is used to open a file for writing. It will automatically create the file if it does not exist and write content to the file. The code is as follows.

<&#63;php
function writefile($fname,$str){
 $fp=fopen($fname,"w");
 fputs($fp,$str);
 fclose($fp);
}
&#63;>
Copy after login

Step 2. Use of functions. For example, create a test.txt file and write the content "abc", the code is as follows:

<&#63;php
$filename='test.txt';
$str='abc';
writefile($filename,$str);
&#63;>
Copy after login

By following the above two steps, you can realize the function of creating files in PHP.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1073121.htmlTechArticlePHP new class problem analysis and solution ideas, php new class ideas, let me first analyze the problems of php new class index .php file function __autoload($_className) {require $_className.'.c...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template