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

Source: Internet
Author: User

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


Let us first analyze the problem of PHP new class

index.php file

function __autoload ($_classname) {require $_classname. Class.php ';} //New 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;//construct method, initialize data public function __construct ($index = ") {$this->index= $index;} UI functions include the corresponding include file Public function UI () {if ($this->index) | |! File_exists ($this->index. Inc ') {$this->index= ' start ';} Return $this->index. Inc ';} }

What is the meaning of the Scarlet letter : The constructor parameter value in the class is set to default is null (Public function __construct ($index = ")), why can not write directly $m=new Main ($_get[' Index");. If you do not want to make a red word in index, the class needs to be written. Thank you, not too understanding.

------to solve the idea----------------------

if (isset ($_get[' index '))) {$m =new Main ($_get[' index ')),//If $_get[' index ' exists then $_get[' index ' as a parameter}else{$m =new main ( ); Otherwise use default parameters}

Direct use of $_get[' index '] will likely raise NOTICE level error

Indiscriminate use of incoming data can lead to security issues

------to solve the idea----------------------

A little change, how do you look.

<?phpclass main{private $index;//constructor method, initialize data public function __construct ($index = ") {$this->index= $index? $index :''; The//ui function includes the corresponding include file Public function UI () {if (Empty ($this->index)

------to solve the idea----------------------

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

ps:php How do I create a file?

PHP project development process, often need to automatically create some files, such as generating static HTML, generate PHP cache files, generate TXT files and so on. Here's how to create a file with a PHP program and write content to a file.

In a project, it is possible to generate files more than once, so we can define a function to call this function when we need to create a file.

Step one, define the function WriteFile, used to open a file in writing, automatically created when the file does not exist, and write the contents to the file, code is as follows.

<?phpfunction WriteFile ($fname, $str) {$fp =fopen ($fname, "w"), Fputs ($fp, $STR); fclose ($fp);}? >

Step two, the use of functions. 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);? >

With the operation of the above two steps, PHP can create the file function.

http://www.bkjia.com/PHPjc/1073121.html www.bkjia.com true http://www.bkjia.com/PHPjc/1073121.html techarticle PHP New class problem analysis and solution ideas, PHP new class of ideas for you to analyze the problem of PHP new class 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.