国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial Share knowledge about some commonly used methods in php

Share knowledge about some commonly used methods in php

Jun 23, 2017 pm 03:20 PM
php abstract method static

Foreword

## OOP

I have been studying PHP for a long time. Today I will summarize the abstract classes and abstract methods in PHP/static properties and static methods/single interest mode (monomorphic mode) in PHP/serialization and deserialization. Rowization (serialization and deserialization).

  
  1. What is an abstract method?
Methods without method bodies {} must be modified with the abstract keyword. Such a method is called an abstract method.
abstract function say (); // Abstract method

2, what is an abstract class?
A class that contains abstract methods is called an abstract class. Abstract classes must be modified with the abstract keyword.
abstract class person {}
## 3. Precautions for abstract categories
① Abstract class can include non -abstract methods; #Does not necessarily include abstract methods;
③ abstract classes, cannot be instantiated.
(The abstract class may include abstract methods, abstract methods have no method, instantiated calls are meaningless.)

The purpose of using abstract class! Just limit instantiation! ! !?? ?

? ? 4. If a subclass inherits an abstract class, the subclass must override all abstract methods of the parent class. Unless the subclass is also an abstract class.
? ? ? ? ? ?
? ? ? 5. What is the role of using abstract classes?
? ? ? ? ? ① Limit instantiation. (An abstract class is an incomplete class. The abstract method inside has no method body, so it cannot be instantiated)
? ? ? ? ? ② Abstract class provides a specification for the inheritance of subclasses. If a subclass inherits an abstract class, it must include and implement the abstract methods defined in the abstract class.
? ? ? ? ? ? ?
? ? ? ? ?

?

##1 Abstract classes and abstract methods in PHP
##2 Static properties and static methods

 
   1. static
   ?、?You can modify attributes and methods, which are called static attributes and static methods respectively, also called class attributes and class methods; name directly.
Use the "class name :: $ static attributes", "class name :: static method ()"
Person :: $ sex; person ::);
③ Static attributes, methods, in It is declared when the class is loaded. Preliminate to the object;
④ Static method, non -static attributes or methods cannot be called;
Non -static method, you can call static attributes or methods; Generated, rather than a static attribute method, no instantiation has been born yet)
? ? ? ? ⑤ In a class, you can use the self keyword to refer to the class name.
Class Person {
Static $ Sex = "nan";
Function say () {
Echo Self :: $ sex;
}
⑥ Static attributes are shared. That is to say, many new objects also share the same attribute.

2. final
① final modified class, this class is the final class and cannot be inherited;
② final modified method, this method is the final method and cannot be overridden!
③ Final properties cannot be modified.
? ? ? ? ? ? ? ? ? ? ? 3. const keyword;
? ? ? ? ? ? ? ? ? When declaring constants in a class, you cannot use the define() function! You must use the const keyword.
and define () are similar to the state, the const keyword declaration cannot be brought with $, and all of them must be uppercase!
? ? ? ? ? ? ? Once a constant is declared, it cannot be changed. When calling, it is the same as static, using the class name Person:: constant.
?
? ? [Small summary] Several special operators
? 1. . ? can only connect strings; "".""
? 2. => declares that the array is associated with the key and value [" key"=>"value"]
3. -> Object (object generated by $this new) uses member attributes and member methods.
4. : : ① Use the parent keyword to call the method of the same name in the parent class; parent::say();
? ? ? ? ② Use the class name (and self) to call the static properties, static methods, and constant.






##3 Single interest mode in PHP (monomorphic mode )

  
 Simple interest mode is also called monomorphic pattern
                              
   by        
    
                        
                       
           
    
    because it can be guaranteed that a class can only have one object instance.

Implementation points:
① The constructor is privatized, and it is not allowed to use the new keyword to create objects.
? ? ? ? ? ② Provide external methods to obtain objects. In the method, determine whether the object is empty. If it is empty, create the object and return it. If it is not empty, put it back directly.

    ③ The attributes of the instance object and the methods of the past objects must be static.

? ? ? ? ? ④ After that, you can only use the static methods we provide to create objects. $s1 = Singleton::getSingle();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

##4 Serialization and Deserialization (Serialization ization and deserialization)

  
                        1. Serialization: The process of converting an object into a string through a series of operations is called serialization; The process of converting a string into an object is called deserialization;
3. When is serialization used?
① When the object needs to be transmitted in the network;
② The object needs to be saved for a long time in the file or database;
4. How to achieve the serialization and anti -stringing of the object?
? ? ? ? ?Serialization: $str = serialize($duixiang);
? ? ? ? ? ?Deserialization: ?$duixiang = unserialize($str); when executing When the object is serialized, the __sleep() function will be automatically executed;
? ? ? ? ? ? ? ② The __sleep() function is required to return an array. The values ??in the array are attributes that can be serialized; attributes that are not in the array cannot be Serialization;
Function __sleep () {
Return Array ("name", "Age"); // Only name/Age can be serialized
}
6, __ wakeupup ()Magic method:
? ? ? ? ? ① When deserializing an object, the __wakeup() method is automatically called;
? ? ? ? ? ? ② When called automatically, it is used to re-copy the new object attributes generated by deserialization;
???????????????????? function __wakeup(){
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

5 Constraint type

  
1. Type constraint: refers to adding a data type before a variable to constrain that the variable can only store the corresponding data type. (This operation is common in strongly typed languages. In PHP, only Can realize type constraints of arrays and objects)
2. If the type constraint is a certain class, this class and subclass objects of this class can pass;
?
3. In PHP, type Constraints can only occur in formal parameters of functions.
class Person{}
class Student extend

function func(Person $p){
//Constraint function parameters, only accept Person class and Person subclass
echo " 111 "
Echo $ P-& GT; name;
}
Func (New Person ()); √
Func (New Student ()); √
Func (" 111 " );



##6 Summary of magic methods

7. __toString(): Automatically called when using echo to print the object. Return the actual content when you want to print the object; the return must be a string; 8. __call(): Automatically called when calling an undefined or unpublicized method in a class. Pass the called function name and parameter list array; 9. __clone(): Automatically called when cloning an object using the clone keyword. The function is to initialize and copy the newly cloned object;
##  1 , __construct(): Constructor, automatically called when new an object 2. __destruct(): Destructor, automatically called before an object is destroyed
3. __get(): Access private information in the class attribute, automatically called. Pass the read attribute name and return $this->Attribute name
4. __set(): Automatically called when assigning a value to a private attribute of the class. Pass the attribute name and attribute value that need to be set
5. __isset(): Automatically called when using isset() to detect the private attributes of the object. Pass the detected attribute name and return isset($this->attribute name);
6. __unset(): Automatically called when using unset() to delete the object's private attributes. Pass the deleted attribute name, and execute unset($this->attribute name) in the method;
10. __sleep(): Automatically called when the object is serialized. Returns an array whose values ??are serializable properties.
11. __wakeup(): Automatically called when the object is deserialized. To deserialize the newly generated object, perform initialization copy;
12. __autoload(): The function needs to be declared outside the class. Automatically called when instantiating a live class. By passing the instantiated class name, the corresponding class file can be automatically loaded using the class name.
?
? ?

?

?

?

There may be some errors in the notes taken while studying, and you are welcome to criticize and give me some advice.

Reflect, review, gain a little bit every day--------------------- Looking forward to a better self

The above is the detailed content of Share knowledge about some commonly used methods in php. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Commenting Out Code in PHP Commenting Out Code in PHP Jul 18, 2025 am 04:57 AM

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Quick PHP Installation Tutorial Quick PHP Installation Tutorial Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

Learning PHP: A Beginner's Guide Learning PHP: A Beginner's Guide Jul 18, 2025 am 04:54 AM

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

Improving Readability with Comments Improving Readability with Comments Jul 18, 2025 am 04:46 AM

The key to writing good comments is to explain "why" rather than just "what was done" to improve the readability of the code. 1. Comments should explain logical reasons, such as considerations behind value selection or processing; 2. Use paragraph annotations for complex logic to summarize the overall idea of functions or algorithms; 3. Regularly maintain comments to ensure consistency with the code, avoid misleading, and delete outdated content if necessary; 4. Synchronously check comments when reviewing the code, and record public logic through documents to reduce the burden of code comments.

Writing Effective PHP Comments Writing Effective PHP Comments Jul 18, 2025 am 04:44 AM

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

Mastering PHP Block Comments Mastering PHP Block Comments Jul 18, 2025 am 04:35 AM

PHPblockcommentsareusefulforwritingmulti-lineexplanations,temporarilydisablingcode,andgeneratingdocumentation.Theyshouldnotbenestedorleftunclosed.BlockcommentshelpindocumentingfunctionswithPHPDoc,whichtoolslikePhpStormuseforauto-completionanderrorche

PHP Development Environment Setup PHP Development Environment Setup Jul 18, 2025 am 04:55 AM

The first step is to select the integrated environment package XAMPP or MAMP to build a local server; the second step is to select the appropriate PHP version according to the project needs and configure multiple version switching; the third step is to select VSCode or PhpStorm as the editor and debug with Xdebug; in addition, you need to install Composer, PHP_CodeSniffer, PHPUnit and other tools to assist in development.

See all articles