PHP Learning PHP-chapter2 features Chapter One PHP code learning PHP to learn more

Source: Internet
Author: User
Tags generator generator traits
  • 2.1 Namespaces:
  • Avoid conflicts and put them in
  • Reference namespaces: Use namespace symphony/httpfoundation;
  • Declaring namespaces: namespace OReilly;
  • Reference a class in the namespace: use Oreilly/con as A;
  • Referencing a function in a namespace: use Func oreilly/functionname;
  • Constants in the reference namespace: User constant Rreilly/const_name;
  • Multi-Import: File header multiple use statements
  • Using multiple namespaces in a file
      • Namespace foo{}
      • Namespace bar{}
  • Global namespaces: Code with no namespace, such as PHP native Exception class, the front home \ Access can do to tell PHP not to look in the current namespace, to find in the global space, $e = new \exception ()
  • Fully qualified PHP class name: (Namespace + class name)
  • 2.2 Using the interface
  • Interface Definition: interface documentable{
    • Public function getId ();
    • Public function getcontent ();
    • }
  • Interface implementation: Class HTMLDocument implements documentable{
    • Public Function _construct () {}
    • Public Function getId () {
      • return $this->url;
    • }
    • Public Function GetContent () {}
  • }
  • 2.3 Character Trait

For reasons of using traits, two classes require a very similar functional structure that, if implemented in an inherited manner, destroys the original class hierarchy and, if implemented using an interface, results in code duplication, thus introducing traits

  • Defining traits: Trait mytrait{
      • The realization of traits
    • }
  • Use of traits: Class myclass{
      • Use mytrait;
    • }
  • 2.4 Generator Generator, iterators
  • The generator is a PHP function, and using the yield keyword, the generator does not return a value, only the output value, only forward iterators, suitable for iterating large datasets.
  • How the generator was created: function Mygenerator () {
      • Yield ' value1 ';
      • Yield ' value2 ';
    • }
  • Use of Generators: Php returns objects of the generator class, saving memory, such as the need to produce an integer in the range of 10000, one way is to create 10,000 integers in memory, and the generator iteration, each time only need to occupy an integer memory.
  • foreach (Mygenerator () as $yieldValue) {
      • echo $yieldValue;//Output value1,value2
    • }
  • 2.5 Closures and anonymous functions
  • Closures: A function that encapsulates the surrounding state when it is created, even if the enclosing environment does not exist, the state of encapsulation in the closure still exists
  • anonymous function: function without name, can pay value to variable
  • Closures and anonymous functions are actually objects, which belong to instances of the closure type
  • 2.6 Creating closures
  • As long as the variable name is followed by (, PHP will look for the _invoke () method, PHP can only make a named callback before closing the packet.
    • $numbersPlusOne = Array_map (function($number) {
      • return $number + 1 ;
      • }, [1,2,3]);
    • Print_r ($numbersPlusOne); // Output - [2,3,4]
  • additional status of closures: BindTo () Alive Use keyword
      • using the Use keyword: function encloseperson ( $name ) {
        • return function ( $doCommand ) use span> ( $name ) {// encapsulates the name parameter
          • return Sprin TF ( '%s,%s ' , $name , $doCommand );
        • };
      • }
      • Use the BindTo () method to append the status of the closure:
        • $this routes [ $routePath ] = $ Routecallback -> bindTo ( $this , _ _class__ );
        • The second parameter is the object type that binds this closure
  • 2.7 Byte code cache Zend Opcache
  • 2.8PHP built-in server

Php-s localhost:4000

If you need to access this server on a different machine, you can set it to Php-s 0.0.0.0:4000

Server configuration: Php-s localhost:4000-c App/config/php.ini

Because the built-in server does not have a. htaccess file, many PHP frameworks are not supported, using the built-in routing script instead

Php-s localhost:4000 router.php

The above describes PHP learning-chapter2 PHP features, including the content of chapter,php learning, I hope that the PHP tutorial interested in a friend helpful.

  • 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.