


PHP learning - Chapter 2: Characteristics of PHP chapter one PHP code learning How to learn PHP more
- 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 ();
- Fully qualified php class name: (namespace + class name)
- 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 ) {
-
($number) { return $number
+ 1
-
return function
- (
-
$doCommand)
use ($name
- ) { //
-
encapsulates the name parameterreturn sprintf('%s, %s',
$name,
- $doCommand
- ); }; } Use bindTo() method to attach Closure status:
$ this - -> routes
[ -
encapsulates the name parameterreturn sprintf('%s, %s',
$name,
- $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
-
$doCommand)
use ($name
- 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.
-
Define traits: trait MyTrait{

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the development of the Internet, the demand for dynamic web pages is increasing. As a mainstream programming language, PHP is widely used in web development. So, for beginners, how to learn PHP development? 1. Understand the basic knowledge of PHP. PHP is a scripting language that can be directly embedded in HTML code and parsed and run through a web server. Therefore, before learning PHP, you can first understand the basics of front-end technologies such as HTML, CSS, and JavaScript to better understand the operation of PHP.

PHP study notes: Web crawler and data collection Introduction: A web crawler is a tool that automatically crawls data from the Internet. It can simulate human behavior, browse web pages and collect the required data. As a popular server-side scripting language, PHP also plays an important role in the field of web crawlers and data collection. This article will explain how to write a web crawler using PHP and provide practical code examples. 1. Basic principles of web crawlers The basic principles of web crawlers are to send HTTP requests, receive and parse the H response of the server.

PHP study notes: Modular development and code reuse Introduction: In software development, modular development and code reuse are very important concepts. Modular development can decompose complex systems into manageable small modules, improving development efficiency and code maintainability; while code reuse can reduce redundant code and improve code reusability. In PHP development, we can achieve modular development and code reuse through some technical means. This article will introduce some commonly used technologies and specific code examples to help readers better understand and apply these concepts.

PHP study notes: Performance analysis and tuning Introduction: In web development, performance is a very critical factor. A high-performance website can provide a better user experience, improve user retention, and increase business revenue. In PHP development, performance optimization is a common and important issue. This article will introduce performance analysis and tuning methods in PHP, and provide specific code examples to help readers better understand and apply these techniques. 1. Performance analysis tool Xdebug extension Xdebug is a powerful P

What is the best way to learn PHP in 2023? With the rapid development of the Internet, computer programming has become a skill with extremely high employment prospects. Among many programming languages, PHP is a language that is widely used in network development. If you want to learn PHP, it's important to know the best way to learn it. PHP is an open source, server-side scripting language used to develop dynamic websites and applications. Compared to other languages, PHP has a lower learning curve and a wide range of applications, making it an ideal choice for beginners.

PHP study notes: Overview of front-end and back-end separation and API design: With the continuous development of the Internet and the increasing user needs, the development model of front-end and back-end separation has attracted more and more attention from developers. Front-end and back-end separation refers to separating the development of the front-end and the back-end, and conducting data interaction through APIs to achieve development efficiency and flexibility. This article will introduce the concept of front-end and back-end separation, and how to design an API. The concept of front-end and back-end separation: The traditional Web development model is front-end and back-end coupling, that is, the development of front-end and back-end is carried out in the same project.

PHP study notes: Form processing and data validation In web development, forms are one of the important components for users to interact with the website. When users fill out forms and submit data on the website, the website needs to process and verify the submitted data to ensure the accuracy and security of the data. This article will introduce how to use PHP to process forms and perform data validation, and provide specific code examples. Form submission and data preprocessing In HTML, we need to use the <form> tag to create a form and specify the form

PHP learning experience: How to handle errors When developing PHP applications, handling errors is a very important aspect. Good error handling can improve the stability and reliability of the code, and can also better help us debug the code and solve problems. This article will introduce some common error types and how to handle them, with corresponding code examples. Syntax Errors Syntax errors are the most common and easiest to find errors in the coding process. It usually causes the PHP parser to not understand the code correctly, resulting in
