Home > php教程 > php手册 > Teacher Shen Yi's special PHP training notes (4)

Teacher Shen Yi's special PHP training notes (4)

WBOY
Release: 2016-08-30 09:21:12
Original
1676 people have browsed it

In the previous lesson, we copied the code through shell script and learned about static methods and static properties. (There is also an accessory PHAR package, but I just ignored it)

Then I wrote some parameters in the GOD file,

 We can also put the method names together like a string.

 <span style="color: #800080;">$get_param</span>=<span style="color: #800080;">$argv</span>[1<span style="color: #000000;">];
 godinit</span>::<span style="color: #800080;">$get_param</span>();
Copy after login

 Okay, let’s follow the teacher’s course to implement the requirements: If the parameter contains “-”, then the description is an attribute. Directly call the static properties of the class. If there is no "-", it means it is a method, and we directly call the static method of the class.

So, I first changed the god file I wrote in the previous class to the following:

<span style="color: #008000;">#</span><span style="color: #008000;">!/usr/local/bin/php</span>
<?<span style="color: #000000;">php

</span><span style="color: #0000ff;">require</span>('god_func7'<span style="color: #000000;">);
</span><span style="color: #0000ff;">require</span>("godinit.php"<span style="color: #000000;">);

    </span><span style="color: #800080;">$result</span>=''<span style="color: #000000;">;
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$argc</span>>=2<span style="color: #000000;">)
    {     
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">'-v'==$argv[1]  && $result=godinit::$V;
        'make'==$argv[1]  && $result=godinit::make();
        'init'==$argv[1] && $result=godinit::init();</span><span style="color: #008000;">*/</span>
        <span style="color: #800080;">$p</span> = <span style="color: #800080;">$argv</span>[1];  <span style="color: #008000;">//</span><span style="color: #008000;">获取参数</span>
        <span style="color: #0000ff;">if</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$p</span>,0,1)=='-')     <span style="color: #008000;">//</span><span style="color: #008000;">  代表获取并匹配变量,</span>
<span style="color: #000000;">        {
          </span><span style="color: #008000;">//</span><span style="color: #008000;">如果传过来的是-v,就会变成v</span>
          <span style="color: #800080;">$p</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$p</span>,1<span style="color: #000000;">);
           </span><span style="color: #800080;">$result</span> = godinit::$<span style="color: #800080;">$p</span><span style="color: #000000;">;
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #800080;">$result</span> = godinit::<span style="color: #800080;">$p</span><span style="color: #000000;">();
        }
    }
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
</span>?>
Copy after login

The result is as shown below:

 

 

 Let’s go back to the command line above. If we randomly output a non-v character, an error will occur, so we still need to improve the code.

<span style="color: #800080;">$p</span> = <span style="color: #800080;">$argv</span>[1];  <span style="color: #008000;">//</span><span style="color: #008000;">获取参数</span>
<span style="color: #0000ff;">if</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$p</span>,0,1)=='-')     <span style="color: #008000;">//</span><span style="color: #008000;">  代表获取并匹配变量,</span>
<span style="color: #000000;">    {
      </span><span style="color: #008000;">//</span><span style="color: #008000;">如果传过来的是-v,就会变成v</span>
        <span style="color: #800080;">$p</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$p</span>,1<span style="color: #000000;">);
        </span><span style="color: #800080;">$result</span> =<span style="color: #0000ff;">isset</span>(godinit::$<span style="color: #800080;">$p</span>)?godinit::<span style="color: #800080;">$p</span>:<span style="color: #000000;">error;
    }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #800080;">$result</span> = godinit::<span style="color: #800080;">$p</span><span style="color: #000000;">();
    }</span>  
Copy after login

 Here we are going to use a PHP magic function __callStatic($m,$args). If you want to use this method, it must be written in the class and must be static. What it does is, if you call an undefined static method, this function will be automatically triggered. The first parameter is the method name, and the second parameter is the method parameter. We can use it for fault tolerance (as long as we learn this point). In the godinit file, we add this method:

<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> '$p1'<span style="color: #000000;">;
}<br></span>
Copy after login

 Then, let’s look at the results:

 

 Now let’s look at the general class definition, instantiation and calling. Let’s create a new godconfig file and create a godconfig class with only attributes

<?<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> godconfig
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$prj_name</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$prj_author</span><span style="color: #000000;">;
    
}
</span>?>
Copy after login

 In the instantiation class godconfig in godinit, use 1. json_encode (class after instantiation): Returns a string in json format 2. json_decode (json string): Returns an object.

<?<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: #800080;">$gc</span> = <span style="color: #0000ff;">new</span> godconfig();          <span style="color: #008000;">//</span><span style="color: #008000;">实例化godconfig里定义的类</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: #800080;">$gc</span> -> prj_name = <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: #800080;">$gc</span> -> prj_author=<span style="color: #008080;">fgets</span><span style="color: #000000;">(STDIN);

        </span><span style="color: #800080;">$ret</span> = <span style="color: #0000ff;">array</span>();                 <span style="color: #008000;">//</span><span style="color: #008000;">初始化一个数组;</span>
        <span style="color: #800080;">$ret</span>[] = <span style="color: #800080;">$gc</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$ret</span><span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">return ""</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>?>
Copy after login

 Then let’s look at the results:

 

A few simple knowledge points:

()? (): ();

substr();

isset();

__callStatic($m,$args) 

json_encode()

json_decode()

Copyright Statement: Note organizer Desperado loves freedom and advocates sharing. But this note is derived from "The First Stage 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.

Previous lesson: Teacher Shen Yi’s special PHP training notes (3)

Next lesson:

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template