PHP New Class problem analysis and solution ideas, php New Class idea _ PHP Tutorial

Source: Internet
Author: User
PHP New Class problem analysis and solution ideas, php New Class ideas. PHP New Class problem analysis and solution ideas, php New Class idea next we will first analyze the php New Class Problem index. php file function _ autoload ($ _ className) {require $ _ className .. c PHP New Class problem analysis and solution ideas, php New Class ideas

Next, let's analyze the problem of creating a php class.

Index. php file

Function _ autoload ($ _ className) {require $ _ className. '. class. php';} // create a class ?? If (isset ($ _ GET ['index']) {$ m = new Main ($ _ GET ['index']);} else {$ m = new Main ();} include $ m-> ui ();

Main. class. php file

Class Main {private $ index; // Constructor that initializes the public function _ construct ($ index = '') {$ this-> index = $ index ;} // The ui function include the corresponding file containing public function ui () {if (empty ($ this-> index) |! File_exists ($ this-> index. '. Inc') {$ this-> index = 'start';} return $ this-> index.'. Inc ';}}

What is the significance of the red part?: The parameter value passed by the constructor in the class has been set to null by default (public function _ construct ($ index = '')), why can't I directly write $ m = new Main ($ _ GET ['index']);. If you do not want to make the if judgment in the red letter in the index, you need to write it in the class. Thank you.

------ Solution ----------------------

If (isset ($ _ GET ['index']) {$ m = new Main ($ _ GET ['index']); // if $ _ GET ['index'] exists, $ _ GET ['index'] is used as the parameter} else {$ m = new Main (); // otherwise, use the default parameter}

Using $ _ GET ['index'] directly may cause a NOTICE-level error.

Using incoming data without additional partitions may cause security problems.

------ Solution ----------------------

Let's take a look.

<? Phpclass Main {private $ index; // Constructor. initialize public function _ construct ($ index = '') {$ this-> index = $ index? $ Index: '';} // The ui function include the public function ui () {if (empty ($ this-> index) file)

------ Solution ----------------------

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

Ps: How does php create files?

During php project development, you often need to automatically create some files, such as generating static html, generating php Cache files, and generating txt files. The following describes how to create a file using a php program and write content to the file.

A file may need to be generated more than once in a project. Therefore, we can define a function and call this function when creating a file.

Step 1. define the writefile function to open a file in write mode. when the file does not exist, it is automatically created and the content is written to the file. the code is as follows.

<?phpfunction writefile($fname,$str){ $fp=fopen($fname,"w"); fputs($fp,$str); fclose($fp);}?>

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

<?php$filename='test.txt';$str='abc';writefile($filename,$str);?>

The above two steps can be used to create files in php.

Next, let's analyze the problem of creating a class in php. index. php file function _ autoload ($ _ className) {require $ _ className. '. c...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.