PHP learning - Chapter 2: Characteristics of PHP chapter one PHP code learning How to learn PHP more

WBOY
Release: 2016-07-29 08:54:11
Original
1144 people have browsed it
  • 2.1 Namespace:
  • To avoid conflicts, put it on the first line after
  • Reference namespace: use namespace Symphony/HttpFoundation;
  • Declare namespace: namespace Oreilly;
  • Reference a class in the namespace: use Oreilly/con as a;
  • Reference a function in the namespace: use func Oreilly/functionName;
  • Reference a constant in the namespace: user constant Rreilly + Bar{}
  • Global namespace: code without a namespace, such as PHP's native Exception class, the previous access can tell PHP not to search in the current namespace, but to search in the global space ,$e = new Exception()
    • Fully qualified php class name: (namespace + class name)
      • 2.2 Using 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 traits trait
    • The reason for using traits is that two classes need very similar functional structures. If implemented through inheritance, it will destroy the original class hierarchy. If implemented using interfaces, it will lead to code duplication, so traits are introduced
      • Define traits: trait MyTrait{
          • // Trait implementation
        • }
      • Trait usage: class MyClass{
          • use MyTrait;
        • }
      • 2.4 Generator, iterator
      • The generator is a php function and uses the yield keyword. The generator does not return a value, only outputs a value, and can only move forward iterator , suitable for iterating large data sets.
      • How to create a generator: function myGenerator(){
          • yield 'value1';
          • yield 'value2'; Usage of generator : PHP returns an object of the Generator class, which is beneficial to saving memory. For example, if you need to generate an integer in the range of 10,000, one way is to create 10,000 integers in memory, and use generator iteration, which only takes up the memory of one integer each time. That’s it.
        • foreach(myGenerator() as $yieldValue){
      • echo $yieldValue;//Output value1, value2
      • }
          • 2.5 Closures and anonymous functions
        • Closure: A function that encapsulates the surrounding state when it is created. Even if the environment where the closure is located no longer exists, the state encapsulated in the closure still exists
      • Anonymous function: A function without a name that can be paid to a variable
      • Closures and anonymous functions are actually objects, instances of the Closure type
      • 2.6 Creating closures
      • As long as there is ( after the variable name, php will look for the _invoke() method. Before there is no closure, php can only Make a named callback
      • $numbersPlusOne
      • =
      • array_map
      • (
          function
        • ($number) { return $number + 1
            ;
          • }, [1,2
          • ,
          • 3]); print_r($numbersPlusOne); //
          • Output
        • -->
        • [2, 3, 4] The additional status of the closure: bindto () Living USE keywords Use use keywords: function enclosePerson($name
        • ) {
      • return function
          (
        • $doCommand) use ($name
            ) { //
          • encapsulates the name parameterreturn sprintf('%s, %s', $name,
              $doCommand
            • ); }; } Use bindTo() method to attach Closure status:
          • $ this
          • ->
          • routes
        • [
        • $routePath
        • ]
        • =
            $routeCallback
          • ->bindTo($this,__CLASS__);The second parameter is to bind this closure object type 2.7 Bytecode cache Zend OPcache2.8PHP built-in server
          • php -S localhost:4000
        If you need to access this server on another machine, OK Set to php -S 0.0.0.0:4000
      • Server configuration: php -S localhost:4000 -c app/config/php.ini
      • Since the built-in server does not have .htaccess files, it does not support many PHP framework, use the built-in routing script instead

      php -S localhost:4000 router.php

      The above has introduced the characteristics of PHP learning - chapter 2 of PHP, including chapter and PHP learning content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!