In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The main content of this article is "introduction to the object-oriented usage of PHP". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to the object-oriented usage of PHP".
object-oriented
In early programming, due to the limitation of computer hardware, programs pursued efficiency while ignoring understandability and expandability. with the development of hardware technology, programming paid more and more attention to multi-person development, and programmers paid more and more attention to program reliability, expansibility and maintainability, so it stimulated the development of program language.
Process oriented
The programmer designs the program code flow chart to assist the program design. Advantages: write functions with whatever functions have disadvantages: data management is confusing, mainly focusing on function facets, object-oriented encapsulation of properties and methods, better reusability and expansibility
object-oriented
Everything is an object, decomposing the transactions that make up the problem into each object, the purpose of establishing the object is not to complete a work, but to describe the behavior of a transaction in solving the problem, which is more in line with people's habits of thinking, high reusability of code and good expansibility.
The core difference between object-oriented and process-oriented is how to assign responsibilities. Classes and objects
Object-oriented is composed of objects with attributes and methods in a system, and the objects interact with each other to achieve business requirements.
Attribute
The variables defined in the class are member properties, data that describes the static properties of the object. Such as a person's name, sex, and lowercase initials
Method
The function is defined as the member method in the class, which is used to describe the operation behavior of the dynamic characteristics of the object. The method name is case-insensitive, non-repeatable, and the first letter is lowercase.
Object life cycle
After creation, the life cycle begins, and when the program ends or the programmer clears the object, PHP automatically destroys the object.
A class is an abstract concept, which is a collection of objects with the same semantic definition (a collective with the same properties and methods). It is not feasible to use specific classes and can only be instantiated. Take the car as an example, the design drawing of the car is the class, and the car is the object. The key point in the design is the creation of classes.
Specification for writing class names
Class name initials are capitalized
A class is defined in a file
$this
You can access properties or methods using the $this pointer in the object.
Class Code {protected $len = 5; public function make () {return $this- > len. $this- > show ();} public function show () {return': is show';}} echo (new Code)-> make (); inheritance
By using extends, you can inherit the properties and methods of the parent class, which is single in PHP.
Class Notify {public function message () {return 'notify message';}} class User extends Notify {} echo (new User)-> message (); the parent class calls
Subclasses can use the parent keyword to call parent class methods
... public function message () {return parent::message ();}. Method rewriting
Subclasses can override the methods of the parent class unless the methods of the parent class are not decorated with final.
Class Notify {public function message () {return 'notify message';}} class User extends Notify {public function message () {return' user notify';}} echo (new User)-> message (); rewriting is prohibited
Using the method declared by final, overriding the parent method in the subclass is prohibited.
Public final function message () {return 'notify message';} encapsulation
Public publicly owned
It can be accessed both inside and outside the class or subclasses, which is the most open permission.
Private is private
Define the properties and methods of a class, which can be accessed inside the class, but not outside the class or subclasses
Protected is protected
Define the properties and methods of a class, which can be accessed inside the class or subclasses, but not outside the class
Module design
Strong cohesion (the function is done within the class as much as possible) and weak coupling (opening up as few methods as possible to external calls). For example, the company sells and receives projects, and the specific work is handed over to the company's internal programmers, designers, and server managers to work together.
Trait
Multiple inheritance can be used in disguise using the trait mechanism.
Class Alipay {use Pay;} class WePay {use Pay;} trait Pay {public function sn () {return 'ABCDEF';}} echo (new WePay)-> sn ()
If there are properties and methods with the same name in this class as in trait, the properties and methods in this class will be used.
... class WePay {use Pay; public function sn () {return _ METHOD__;}} trait Pay {public function sn () {return 'ABCDEF';}}.. Multiple trait
You can use multiple trait to connect with commas
... use Pay,Site;... Resolve conflicts class WePay {use Pay, Email {Pay::notify insteadof Email; Email::notify as EmailNotify;} trait Pay {public function notify () {return _ METHOD__;}} trait Email {public function notify () {return _ METHOD__;}} echo (new WePay)-> notify ()
Pay::notify insteadof Email means to use the Pay::notify method instead of the Email::notify method.
Email::notify as EmailNotify` aliases `EmailNotify` as `EmailNotify access control
Access control can be redefined for inherited trait methods
Class WePay {use Pay, Email {Pay::notify insteadof Email; Email::notify as protected EmailNotify;...} multiple trait
It can be used through multiple trait combinations.
Trait Notify {public function response () {return 'notify response';}} trait Pay {use Notify;} class User {use Pay;} echo (new User)-> response (); abstract method trait Notify {public function response () {return' notify response'. $this- > sn ();} abstract protected function sn ();} trait Pay {use Notify;} class User {use Pay; protected function sn () {return 'SN999';}} echo (new User)-> response (); static method
Static methods, abstract methods, and static properties can be used in trait.
... trait Pay {public function sn () {return 'ABCDEF';} public static function notify () {return _ METHOD__;}} class WePay {use Pay;...} echo WePay::notify (); static
Static:
A data object is required to serve only the class, that is, when the class is available internally but not externally. Building an object is extremely resource-consuming, so when a method has strong commonality, there is no need to regenerate an instance of the class in order to call the method. The defined method or variable resides in memory when the program is loaded for the first time, and the program is released at the end.
Static variable:
The member variables declared by static are static variables or class variables, which are public variables of this class and are generated when they are used for the first time. There is only one copy of all the objects of this class, which belongs to the class, not to the object. The static variable belongs to the class but not to the object. It can be accessed by the class anywhere. It is the global variable of the class and is stored in memory when the class is created. For multiple objects, static data members are stored in only one place, saving memory. Update the value of the static data member only once to ensure that all objects access the same value after the update.
Static method:
Methods declared with static are static methods or class methods, and object references are not passed to functions when the method is executed, so we cannot access non-static members, only static methods or static variables. You can only use methods about classes, such as self static parent, etc. It can be executed without generating an object when in use.
Quasi constant
Use const to define class constants, and constants are called using self::.
Class Model implements ArrayAccess, Iterator {use ArrayIterator, Relation, Validate, Auto, Filter; /-automatic verification-/ / verify const EXIST_VALIDATE = 1 when there is a field; / / verify const NOT_EMPTY_VALIDATE = 2 if the value is not empty;.} $this self:: parent::
$this
Is a reference to the current object, which usually appears in a method and is used to get a member property of a class, or to execute a member method of a class
Self::
A reference to this class that is used to get the declarative member property or static member method self::run () of the current class
Parent::
A reference to the parent class, calling the methods or properties of the parent class.
The Construction method of Magic method & destructing method
Construction method _ _ construct ()
It is automatically executed when the object is created, with no return value, and is used to perform some initialization of the class, such as the initialization of object properties, with the constructor _ _ construct ().
You can pass parameters in the constructor to define properties, and execute the constructor of the subclass when both the parent class and the subclass define the constructor
Destructing method _ _ destruct ():
Executes when references to all objects are destroyed.
_ _ get and _ _ set
_ _ get () is called when reading inaccessible or nonexistent attributes, and the _ _ set () method is executed when getting inaccessible or nonexistent attributes.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.