Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-08-29 08:36:48
Original
1324 people have browsed it

1. Since we put the program in /usr/local/bin in the last two classes. Each edit requires sudo. In this lesson, we use PHPSTORM to edit the code, copy it specifically, and then put it in a folder called home/godpro.

2. Now let’s make a copy script to automatically copy files under home/godpro to /usr/local/bin

 1. We create a file called done under godpro.

<span style="color: #0000ff;">echo</span> your password | sudo -S /bin/bash -c ' cp god* /usr/local/bin'
Copy after login
<span style="color: #0000ff;">然后给这个done    chmod</span> +x <span style="color: #0000ff;">done</span>
Copy after login

3. In this class, you will learn about PHP classes

 <span style="color: #0000ff;">class</span><span style="color: #000000;"> godInit
{
   </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$god_version</span>=<span style="color: #000000;">“”;
   </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> genConfig() { ……}
}</span>
Copy after login

 Today we first learn how to write static methods and static properties.

4. We set a requirement: when we execute godinit, our God will ask you questions: 1. What is the name of your project? 2. Ask who is the author of the project? After answering, save these 2 values ​​​​in godinit2 static variables:

 The first knowledge point that needs to be supplemented is: getting the characters entered by the user from the standard input $getstr=fgets(STDIN);

Okay, let’s create a new file godinit and create a class:

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

</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;">$VERSION</span>="god version is 1.1";   <span style="color: #008000;">//</span><span style="color: #008000;">声明一个静态属性$VERSION</span>
    <span style="color: #0000ff;">static</span> <span style="color: #800080;">$prj_name</span>="";                    <span style="color: #008000;">//</span><span style="color: #008000;">声明一个静态属性项目名称</span>
    <span style="color: #0000ff;">static</span> <span style="color: #800080;">$prj_author</span>='shenyi';            <span style="color: #008000;">//</span><span style="color: #008000;">声明一个静态属性项目作者</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: #0000ff;">echo</span> "input your project name?".<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
        self</span>::<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;">;
        self</span>::<span style="color: #800080;">$prj_author</span>=<span style="color: #008080;">fgets</span><span style="color: #000000;">(STDIN);

        </span><span style="color: #0000ff;">echo</span> "采集信息如下".<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> self::<span style="color: #800080;">$prj_name</span>.<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> self::<span style="color: #800080;">$prj_author</span>.<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
        </span><span style="color: #008000;">//</span><span style="color: #008000;">return ""</span>
<span style="color: #000000;">    } 
}
</span>?>
Copy after login

 If we want to change static variables inside a class, we can use self; self represents the class itself, and :: represents calling the variables or functions of the class itself.

Now the god file should be:

<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>("godint.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>'-v'==<span style="color: #800080;">$argv</span>[1]  && <span style="color: #800080;">$result</span>=godinit::<span style="color: #800080;">$VERSION</span><span style="color: #000000;">;
        </span>'make'==<span style="color: #800080;">$argv</span>[1]  && <span style="color: #800080;">$result</span>=godinit::<span style="color: #000000;">make();
        </span>'init'==<span style="color: #800080;">$argv</span>[1] && <span style="color: #800080;">$result</span>=godinit::<span style="color: #000000;">init();
    }


</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

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 (2)

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