Home >php教程 >php手册 >Teacher Shen Yi's special PHP training notes (7)--What's my name?

Teacher Shen Yi's special PHP training notes (7)--What's my name?

WBOY
WBOYOriginal
2016-09-02 08:42:531797browse

1. Generate folders. ​

mkdir();--New directory

bool <span style="color: #008080;">mkdir ( <span style="color: #0000ff;">string <span style="color: #800080;">$pathname [, int <span style="color: #800080;">$mode = 0777 [, bool <span style="color: #800080;">$recursive = <span style="color: #0000ff;">false [, <span style="color: #0000ff;">resource <span style="color: #800080;">$context ]]] )<br>//尝试新建一个由 pathname 指定的目录。</span></span></span></span></span></span></span></span>

 Parameters: pathname: The path to the directory.

 mode: The default mode is 0777, which means the maximum possible access rights. For more information about mode please read the chmod() page.

Did you see the function above? Remember it. Teacher Shen left a homework in the last class, which was to read the god.json file and generate the simplest "skeleton". 1. Accept parameter start. 2. Generate a folder based on the value of prj_name. 3. An index.php is generated by default in the newly created folder.

  OK, let’s implement the first step and write a method in the godinit file

 <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> start(){
        </span><span style="color: #800080;">$get_config</span> =<span style="color: #000000;"> loadConfig();
        </span><span style="color: #008080;">mkdir</span>(<span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$get_config</span>-><span style="color: #000000;">prj_name);
    }</span>


  Then ./god start, a project folder is generated in my file path. Of course, this is a course demonstration. We also need to determine whether the folder already exists. If it does not exist, create it. If it exists, do not create it.

Improve this start() method:

<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> start(){
        </span><span style="color: #800080;">$get_config</span> =<span style="color: #000000;"> loadConfig();
        </span>!<span style="color: #008080;">file_exists</span>(<span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$get_config</span>->prj_name) && <span style="color: #008080;">mkdir</span>(<span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$get_config</span>-><span style="color: #000000;">prj_name);
    }</span>

  A function appears above: file_exists();--Check whether the file or directory exists. Let’s strengthen it again:

bool <span style="color: #008080;">file_exists</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">检查文件或目录是否存在。</span>

 Parameters: filename The path of the file or directory.

  Return value: If the file or directory specified by filename exists, return TRUE, otherwise return FALSE .


We continue to return to the course and generate a PHP file:

<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> start(){
  </span><span style="color: #800080;">$get_config</span> =<span style="color: #000000;"> loadConfig();
   </span><span style="color: #008000;">//</span><span style="color: #008000;">判断并生成新的文件夹,没有就创建</span>
   !<span style="color: #008080;">file_exists</span>(<span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$get_config</span>->prj_name) && <span style="color: #008080;">mkdir</span>(<span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$get_config</span>-><span style="color: #000000;">prj_name);
   </span><span style="color: #008000;">//</span><span style="color: #008000;">在该文件夹下判断并生成一个index.php文件,没有就创建</span>
    !<span style="color: #008080;">file_exists</span>(<span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$get_config</span>->prj_name."/index.php") && <span style="color: #008080;">file_put_contents</span>(<span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$get_config</span>->prj_name."/index.php",""<span style="color: #000000;">);
    }</span>

 OK, the homework of the previous class is completed.


The main content of this lesson is to create a new god_frame.php and write a class specifically for handling skeletons.

Since god is used as the “skeleton”. Then we need to create a constructor first, and pre-book 1. the folder name of the skeleton and 2. the entry file of the skeleton. Then we have to create a folder core that represents the god core, create a subfolder called frame below, and then create a folder called template under the frame. Finally, stuff god_frame.php under the frame.

Before writing the code, we need to strengthen a magic function:

 __autoload();-- Trying to load an undefined class

void __autoload ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$class</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">你可以通过定义这个函数来启用类的自动加载。</span>

 Parameter: class--The name of the class to be loaded

<span style="color: #008000;">//</span><span style="color: #008000;">示例
//尝试加载 未定义的类,如果加载了未定义的类,则会自动进入这个函数(如果你写了的话)</span>
 <span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$classname</span>) <span style="color: #008000;">//</span><span style="color: #008000;">接收一个参数</span>
<span style="color: #000000;">{
     </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$classname</span><span style="color: #000000;">; 通过这可以发现,会把你尝试加载的类的“类名”获取到
 }</span>

  然后我们今天还要研究下命名空间namespace。然后用use导入命名空间。

  好了,我们来看看我这节课完成的代码:god_frame.php 

<?<span style="color: #000000;">php
namespace core\frame;
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> god_frame
{
    </span><span style="color: #0000ff;">public</span>  <span style="color: #800080;">$project_folder</span> = '';       <span style="color: #008000;">//</span><span style="color: #008000;">项目文件夹</span>
    <span style="color: #0000ff;">public</span>  <span style="color: #800080;">$project_main</span> = '';         <span style="color: #008000;">//</span><span style="color: #008000;">入口文件</span>
    <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$prjName</span>){      <span style="color: #008000;">//</span><span style="color: #008000;">构造函数</span>
       <span style="color: #800080;">$this</span>->project_folder = <span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$prjName</span><span style="color: #000000;">;
       </span><span style="color: #800080;">$this</span>->project_main = <span style="color: #800080;">$this</span> ->  project_folder."/index.php"<span style="color: #000000;">;

    }
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> run(){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">判断并生成新的文件夹,没有就创建</span>
        !<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$this</span>->project_folder) && <span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">project_folder);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">在该文件夹下判断并生成一个index.php文件,没有就创建</span>
        !<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$this</span>->project_main ) && <span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$this</span>->project_main,""<span style="color: #000000;">);
    }
}
</span>?>
<span style="color: #008000;">//</span><span style="color: #008000;">godinit</span>
<?<span style="color: #000000;">php
</span><span style="color: #008080;">define</span>('cstring','json'<span style="color: #000000;">);
</span><span style="color: #0000ff;">require</span>('godconfig.php');                    <span style="color: #008000;">//</span><span style="color: #008000;">引入gonconfig这个文件</span>
<span style="color: #0000ff;">use</span><span style="color: #000000;"> core\frame;
</span><span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$className</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$className</span> = <span style="color: #008080;">str_replace</span>('\\','/',<span style="color: #800080;">$className</span>).'.php'<span style="color: #000000;">;
    </span><span style="color: #0000ff;">require</span> (<span style="color: #800080;">$className</span><span style="color: #000000;">);
}
</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: #0000ff;">echo</span> "input your project name?".<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
        </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;">;
        </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>
      genConfig(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;">function</span><span style="color: #000000;"> ini(){
       </span><span style="color: #800080;">$get_config</span> =<span style="color: #000000;"> loadConfig();
        </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$get_config</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: #0000ff;">echo</span> <span style="color: #800080;">$k</span>.":".<span style="color: #800080;">$v</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> start(){
        </span><span style="color: #800080;">$get_config</span> =<span style="color: #000000;"> loadConfig();
        </span><span style="color: #800080;">$gf</span> = <span style="color: #0000ff;">new</span> god_frame(<span style="color: #800080;">$get_config</span>-><span style="color: #000000;">prj_name);
        </span><span style="color: #800080;">$gf</span> -><span style="color: #000000;"> run();
    }
</span><span style="color: #008000;">/*</span><span style="color: #008000;">    static  function make()
    {
        $pchar=new Phar("god.phar");
        $pchar->buildFromDirectory(dirname(__FILE__));
        $pchar->setStub($pchar->createDefaultStub('god'));
        $pchar->compressFiles(Phar::GZ);
    }</span><span style="color: #008000;">*/</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>?>

 

 版权声明:笔记整理者亡命小卒热爱自由,崇尚分享。但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的《 PHP魔鬼训练课第一阶段》。本学习笔记小卒于博客园首发, 如需转载请尊重老师劳动,保留沈逸老师署名以及课程来源地址。

 

 上一课:沈逸老师PHP魔鬼特训笔记(6)--巫术与骨架

 

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