Home  >  Article  >  php教程  >  Teacher Shen Yi’s special PHP training notes (5)

Teacher Shen Yi’s special PHP training notes (5)

WBOY
WBOYOriginal
2016-08-30 09:21:091530browse

  Following the previous lesson, we created a new godconfig class and set two attributes prj_name (project name) and prj_author (author). Then we obtained the standard input (stdin) and saved the result in the class.

 Okay, the name of this lesson is rather weird - I have "lazy cancer". If we forget to write the attributes of the godconfig class, will our program still run? The answer is still possible, PHP will automatically add attributes to your class (this is the original words of the teacher's PPT).
GOON, continue to be lazy, now I don’t want to create the godconfig class anymore. Here we use $gc = new stdClass();

 The previous point is very simple, so I won’t post the code and running results.

Next, we have another practical requirement: we write a method in the function file, functionTC() //The meaning of temp class (the name is chosen by the teacher at will). Represents a temporary class returned.

The first way to write:

<?<span style="color: #000000;">php

</span><span style="color: #0000ff;">require</span>('godconfig.php');                    <span style="color: #008000;">//</span><span style="color: #008000;">引入gonconfig这个文件</span>
<span style="color: #0000ff;">class</span> godinit                                <span style="color: #008000;">//</span><span style="color: #008000;">创建一个类,godinit</span>
<span style="color: #000000;">{
    </span><span style="color: #0000ff;">static</span>  <span style="color: #800080;">$v</span>="god version is 1.2";         <span style="color: #008000;">//</span><span style="color: #008000;">声明一个静态属性$VERSION</span>

    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> init()                   <span style="color: #008000;">//</span><span style="color: #008000;">静态方法 init</span>
<span style="color: #000000;">    {
        </span><span style="color: #008000;">//</span><span style="color: #008000;">$gc = new godconfig();             //实例化godconfig里定义的类
        //$gc = new stdClass();</span>
        <span style="color: #0000ff;">echo</span> "input your project name?".<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
        </span><span style="color: #008000;">//</span><span style="color: #008000;">$gc -> prj_name = fgets(STDIN);            //从标准输入中获取用户输入的字符并赋值给实例化属性$prj_name
        <span style="color: #800080;">$prj_name</span> = <span style="color: #008080;">fgets</span>(STDIN);           <span style="color: #008000;">//</span><span style="color: #008000;">重新获取用户输入,并赋值给$prj_name</span>
        
        <span style="color: #0000ff;">echo</span> "input your author name?".<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
        </span><span style="color: #008000;">//</span><span style="color: #008000;">$gc -> prj_author=fgets(STDIN);</span>
        <span style="color: #800080;">$prj_author</span> = <span style="color: #008080;">fgets</span>(STDIN);         <span style="color: #008000;">//</span><span style="color: #008000;">重新获取用户输入,并赋值给$prj_author</span>

        <span style="color: #0000ff;">echo</span> json_encode(TC(<span style="color: #0000ff;">array</span>('prj_name'=><span style="color: #800080;">$prj_name</span>,'prj_author'=><span style="color: #800080;">$prj_author</span><span style="color: #000000;">)));
    }</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> __callStatic(<span style="color: #800080;">$p1</span>,<span style="color: #800080;">$p2</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">echo</span> "error function"<span style="color: #000000;">;
    }
}
</span>?>

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

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">functions for PHP7</span>
<span style="color: #0000ff;">function</span> genConfig(<span style="color: #800080;">$cnt</span>):<span style="color: #0000ff;">string</span><span style="color: #000000;">
{
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">file_put_contents</span>(<span style="color: #008080;">getcwd</span>().'/god.json',<span style="color: #800080;">$cnt</span>).' of bytes is written.'.<span style="color: #ff00ff;">PHP_EOL</span>.'god config is created'<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">function</span> TC(<span style="color: #800080;">$p</span><span style="color: #000000;">){
    </span><span style="color: #ff0000;">$get_class = new</span><span style="color: #000000;"><span style="color: #ff0000;"> stdClass();</span>
    </span><span style="color: #ff0000;">foreach ($p as $k => $v){
        $get_class -> $k=$v;
    }
    return $get_class<span style="color: #000000;"><span style="color: #ff0000;">;</span>
}</span>

 Then let’s take a look at the result. It’s still exactly the same as the original writing method:

 

 OK, now you can realize godconfig without having to build it.

There is a second way to write it: I am too lazy to remember it. Well, this is real laziness.

  The third way of writing is related to PHP7 anonymous classes.

 The constructor in PHP is __construct(parameter) {}. This function will first be executed when the class is instantiated. Next, let's look at the anonymous classes of PHP7 (actually there is no mystery, the so-called anonymous means that it has no name). The only difference is that if the anonymous class wants to use external variables, it needs to be passed in, otherwise it cannot be used.

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">functions for PHP7</span>
<span style="color: #0000ff;">function</span> genConfig(<span style="color: #800080;">$cnt</span>):<span style="color: #0000ff;">string</span><span style="color: #000000;">
{
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">file_put_contents</span>(<span style="color: #008080;">getcwd</span>().'/god.json',<span style="color: #800080;">$cnt</span>).' of bytes is written.'.<span style="color: #ff00ff;">PHP_EOL</span>.'god config is created'<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">function</span> TC(<span style="color: #800080;">$p</span><span style="color: #000000;">){
     </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">class</span>(<span style="color: #800080;">$p</span>){          <span style="color: #008000;">//</span><span style="color: #008000;">this is 匿名类</span>
         <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$p</span><span style="color: #000000;">){
             </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$p</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span> => <span style="color: #800080;">$v</span><span style="color: #000000;">){
                 </span><span style="color: #800080;">$this</span> -> <span style="color: #800080;">$k</span> = <span style="color: #800080;">$v</span><span style="color: #000000;">;
             }
         }
     };
}</span>

 In fact, the above is not lazy at all, writing so much code^_^;

Let’s take a look at the results:

 

 

Copyright Statement: Note organizer Desperado loves freedom and advocates sharing. But this note is derived from "The First Phase of PHP Devil Training Course" by teacher Shen Yi of www.jtthink.com (Programmer on the Road). This study note was first published on the blog. If you need to reprint it, please respect the teacher's work and retain the signature of Teacher Shen Yi and the course source address.

Essential knowledge points:

1. stdClass: PHP’s built-in top-level class, which is actually a base class and blank. Nothing. If you use it temporarily, it will rarely be used elsewhere. Then there is no need to create any new entity classes at all. Just create a new stdClass directly.
2. foreach traverses the array

3. Anonymous class:  

Statement:
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