Judging teacher PHP Devil Special Training Note (5)

Source: Internet
Author: User

 Next lesson, we created a new Godconfig class, set two properties Prj_name (project name), Prj_author (author), and then we get the standard input (stdin) to save the results in the class.

Well, the name of this class is quite wonderful-I have "lazy cancer", if we forget to write the properties of the Godconfig class, will our program run? The answer is yes, and PHP will automatically add attributes to your class (this is the teacher PPT quote).
GOON, continue to lazy, now Godconfig class do not want to create. Here we use $GC = new StdClass ();

The point ahead is simple, I won't post the code and run the results.

Below, we have another practical need: We write a method in the function file, FUNCTIONTC ()//temp class meaning (the name is the teacher random). The delegate returns a temporary class.

The first type of notation:

<?PHPrequire(' godconfig.php ');//introduce gonconfig this fileclassGodinit//create a class, Godinit{    Static  $v= "God version is 1.2";//declares a static property $version    Static functionInit ()//static method Init    {        //$GC = new Godconfig (); Instantiate the class defined in Godconfig//$GC = new StdClass ();        Echo"Input your project name?".Php_eol; //$GC-prj_name = fgets (STDIN); Obtains the user input character from the standard input and assigns the value to the instantiated attribute $prj_name        $prj _name=fgets(STDIN);//re-fetch user input and assign value to $prj_name                Echo"Input your author name?".Php_eol; //prj_author=fgets, $GC (STDIN);        $prj _author=fgets(STDIN);//re-fetch user input and assign value to $prj_author        EchoJson_encode (TC (Array(' prj_name ' =$prj _name, ' prj_author ' =$prj _author))); }    Static function__callstatic ($p 1,$p 2){        Echo"Error Function"; }}?>

  At the same time, we write a TC method in God_func7 this file:

<?PHP//functions for PHP7functionGenconfig ($cnt):string{    return file_put_contents(GETCWD().‘ /god.json ',$cnt). ' of bytes is written. '.Php_eol.‘ God config is created ';}functionTC ($p){    $get _class = newStdClass ();    foreach ($p as $k = $v) {$get _class $k = $v; } return $get _class;}

  Then we look at the results, or the same as the original result of a hair:

  

OK, now you can implement the Godconfig are not built.

There is a second way of writing: I have to remember the lazy ball. Well, that's the real lazy.

   The third way of writing is related to the PHP7 anonymous class.

The constructor in PHP is __construct (parameter) {}, and the class executes the function first when it is instantiated. Next we look at PHP7 's anonymous class (in fact, nothing mysterious, so-called Anonymous is no name only). Only the difference is that if an anonymous class is to use an external variable, it needs to be passed in, otherwise it cannot be used.

<?PHP//functions for PHP7functionGenconfig ($cnt):string{    return file_put_contents(GETCWD().‘ /god.json ',$cnt). ' of bytes is written. '.Php_eol.‘ God config is created ';}functionTC ($p){     return New class($p){//This is anonymous class         function__construct ($p){             foreach($p  as $k=$v){                 $this-$k=$v; }         }     };}

  In fact, the above is not lazy, write so many code ^_^;

Let's look at the results:

  

 

Copyright NOTICE: NOTE-taker fugitive pawns love freedom, advocating sharing. But this note stems from www.jtthink.com (programmer in the awkward way) judging teacher's "PHP Devil training First stage." This study note pawn in the blog Park debut, if need to reprint please respect the teacher Labor, retain judging teacher attribution and course source address.

The point of knowledge of evil:

1, stdclass:php built-in top-level class, is actually a base class, blank. There's nothing. If you use it temporarily, it is seldom used elsewhere. Then you don't need to create any entity classes at all. Direct new to a stdclass is all right.
2. Foreach Traversal array

3. Anonymous class: 

Judging teacher PHP Devil Special Training Note (5)

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.