MordenPHP reading notes (1) -- run first, then run tired, and then run mordenphp first _ PHP Tutorial

Source: Internet
Author: User
Tags rewind
MordenPHP reading notes (1) -- run first, then run tired, and then run mordenphp first. MordenPHP reading notes (1) -- first run and then run tired, then run mordenphp first --- restore content start --- a lot of semi-finished products in the background, or almost impossible... This book does not take MordenPHP reading notes (1) -- run it first, then run it tired, and then run mordenphp first

--- Restore content start ---

There are a lot of semi-finished products in the background, or they are almost impossible...

This book is good. at least it is recommended by others, and then it is quite new. the key to learning which one is not learning is to read it.

Today, the Internet is not good, and the code required for scientific research is not available. read and take notes.

This book basically involves some new changes after version 5.4, which are easy to understand. even though you are not easy to go, you can't go anywhere when you run it, when I get tired, I have a chance to go ~~

(1) features

I. namespace

A file is a class and namespace is used for mutual calls;

1 // 2 // Namespace 3 // 4 namespace ModernPHP \ feature \ mingmingkongjian; 5 function var_dump () {6 echo "Shit! "."
"; 7} 8 9 $ test =" OK "; 10 var_dump ($ test); 11 \ ModernPHP \ feature \ mingmingkongjian \ var_dump (); 12 13 // The namespace must start with the header, but a file can have many namespaces, and then there can be a sub-space 14 // the namespace of the vendor is the top-level namespace, used to identify the brand 15 // to solve the naming conflict problem, of course, there should be more flexible usage 16 17 // A more practical point: import and alias 18 // import the class definition in another folder, directly use 19 require 'index. php '; 20 use a \ aaa; 21 $ daoru = new aaa; 22 $ daoru-> send (); 23 // use is import, then, set the lazy alias 24 // in use. In addition, the use function 25 // use func a \ call; 26 // \ a \ call () can be implemented after version 5.6 ();

Index. php

1
 

II. use interfaces

Interface, I didn't understand it too well. After I understood it, it was awesome!

An interface that everyone can use as long as they comply with the interface rules.

The following is an interface example for obtaining content. you can also write more modules based on this interface. (in this example, none of the getContent modules can be used... Cry)

  GetID (); $ value = $ document-> getContent (); $ this-> data [$ key] = $ value;} public function getDocuments () {return $ this-> data ;}} interface Documentable {// defines the interface. to put it bluntly, set the rules. to use them elsewhere, you have to say public function getId (); public function getContent ();} class HtmlDocument implements Documentable {// declare the interface to use; this is the protected $ url for obtaining the url content; public function _ construct ($ url) {$ this-> url = $ url;} public function getId () {return $ t His-> url;} public function getContent () {$ ch = curl_init (); // Here, curl is used to operate a library (equivalent to) on the url ). This command is used to enable a curl dialog, so the following is a dialog curl_setopt ($ ch, CURLOPT_URL, $ this-> url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ ch, CURLOPT_MAXREDIRS, 3); $ html = curl_exec ($ ch ); // run this command to execute the preceding dialog curl_close ($ ch); return $ html ;}$ documentStore = new DocumentStore (); $ htmlDoc = new HtmlDocument (' http://www.baidu.com '); $ DocumentStore-> addDocument ($ htmlDoc); print_r ($ documentStore-> getDocuments ());

Another module

1 class StreamDocument implements Documentable {// streaming media 2 protected $ resource; 3 protected $ buffer; // buffer size 4 5 public function _ construct ($ resource, $ buffer = 4096) {6 $ this-> resource = $ resource; 7 $ this-> buffer = $ buffer; 8} 9 10 public function getId () {11 return 'resource -'. (int) $ this-> resource; 12} 13 14 public function getContent () {15 $ streamContent = ''; 16 rewind ($ this-> resource ); // The rewind () function returns the bit of the file pointer. Set 17 while (feof ($ this-> resource) === false) at the beginning of the inverted file {// feof () function to check whether it has reached the end of the file (eof ). 18 $ streamContent. = fread ($ this-> resource, $ this-> buffer); 19} 20 21 return $ streamContent; 22} 23}

III. traits

Something strange...

In fact, it is for multi-inheritance or one to multiple different classes.

1
  "; 15} 16} 17 18 trait traitMore {19 public function testAgain () {20 echo" This is multiple use ."."
"; 21} 22} 23 24 class className {25 use traitName; 26 use traitMore; 27 28} 29 30 $ classMine = new className (); 31 $ classMine-> testThis (); 32 $ classMine-> testAgain ();

IV. generator

Directly add code

1
  "; 29} 30 // when the data file is large, the effect is particularly obvious

5. closure

Here closures are basically equivalent to anonymous functions.

1
  

6. Additional status

I don't understand this...

(2) standards

Some conventions of PHP-FIG;

--- Class name, hump type, ShitHappens

--- Method name, hump type, but lowercase letter, shitHappens

--- Four spaces are indented.

--- Do not write?> End symbol;

--- {Start another line;

--- The namespace must have spaces;

--- Attributes and methods in the class must have a visibility declaration;

--- If and other control structures are followed by spaces;

 1 
   a===false){19             return true;20         }21     }22 }

----------------------

I will talk about things later. if you need something, I will try again.

Skip (1) -- run first, then run tired, then run mordenphp first --- restore content start --- a lot of semi-finished products in the background, or almost impossible... This book is not...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.