search
Homephp教程php手册Teacher Shen Yi's special PHP training notes (3)

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'
<span style="color: #0000ff;">然后给这个done    chmod</span> +x <span style="color: #0000ff;">done</span>

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>

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

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

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)

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor