Home > php教程 > php手册 > body text

Compilation of knowledge points about object-oriented objects in PHP

WBOY
Release: 2016-09-11 11:19:39
Original
839 people have browsed it

Object-oriented

Everything is an object. Decompose the transactions that constitute the problem into each object. The purpose of establishing an object is not to complete a job, but to describe the behavior of a certain transaction in solving the problem. This is more in line with human thinking habits and code reusability. High, scalability.
__________________________________________________________

The concept of class

Category
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 a car as an example. The design drawing of the car is the class, and the car is the object. The focus in design is the creation of classes
The concept of classes in real life
Everything is an object. If the specific computer we use is an object, then the computer is a class. You go to the kindergarten to pick up your child. If you say you want to pick up your child, they will definitely not give it to you (unless they know you and know who your child is). You have to name your child. Your child is the specific target, and the child is a person. Class
Class name writing specifications
The first letter of the class name is capitalized. A class is defined in a file and ends with .class.php as the file name
__________________________________________________________
Object
Object handle
Used to distinguish different objects. After the object is created, a storage space is obtained in the memory. The address of the storage space is the identifier or handle of the object
Properties
Variables defined in a class are member attributes, used to describe the data of the static characteristics of the object. Such as a person’s name, gender, first letter in lowercase
Method
A function defined in a class is a member method, which is used to describe the operating behavior of the dynamic characteristics of the object. Method names are not case-sensitive, cannot have duplicate names, and the first letter is lowercase
Object life cycle
After creation, the life cycle begins. When the program ends or the programmer clears the object, it will be destroyed. PHP will automatically destroy the object

Object life cycle

1 After the program execution is completed, PHP runs the recycling mechanism and deletes the objects in the memory
2 Delete all
After the object is referenced, the object is garbage, and the recycling mechanism automatically deletes the garbage

Steps of object generation

1 Create space for objects in content
2 Execute the construction method
3 Return the reference address of the object

Memory distribution of objects:

1 The object reference is placed in the "stack memory" (the stack stores fixed content)
2 Objects are placed in "heap memory" (the heap stores variable content)
3 Static members are placed in the "data area", which are placed when they are loaded for the first time, so that they can be shared by every object in the heap memory
4 Functions and methods are placed in the code area


Object-oriented features

Abstractness
Abstract the common attributes and methods of a class of objects to form a class. This way of thinking is abstract
Encapsulation:
Encapsulate member methods and member attributes into classes, hide attributes and methods, hide method implementation details, and limit access rights to class members through public protected private final static. Data is protected internally and can only be accessed through authorized member methods. Can be operated. Encapsulate members as much as possible
Inheritance extends:
You can make a class inherit and have the member attributes and methods of another existing class. The inherited class is called the parent class or base class, and the inherited class is a subclass. extends keyword implements inheritance relationship
Polymorphism:
The subclass inherits the parent class and achieves polymorphism by overriding the parent class methods
______________________________________________________________________________

Access modifier (scope descriptor)

public
It can be accessed both inside and outside the class or subclasses, which is the most open permission
private
The attributes and methods of the defined class can be accessed inside the class, but cannot be accessed outside the class or subclasses
protected
Define the attributes and methods of a class, which can be accessed inside the class or subclasses, but not accessible outside the class
Module design
Strong cohesion (functions should be completed within the class as much as possible), weak coupling (open as few methods as possible for external calls). Example: The company's sales team receives projects, and the specific work is left to the company's internal programmers, designers, and server managers to complete collaboratively
______________________________________________________________________________

static static properties and static methods

A data object is required to only serve the class, that is, when the class is available internally but not externally. Creating objects is extremely resource-consuming, so when a method has strong publicity, there is no need to regenerate an instance of the class in order to call this method. The defined methods or variables reside in memory when the program is loaded for the first time and are released when the program ends.
Static methods cannot be overridden by non-static methods, and constructors are not allowed to be declared static
static variable:
Member variables declared through static are static variables or class variables. They are public variables of the class. They are generated when used for the first time. There is only one copy for all objects of the class. They belong to the class, not to the object. . Static variables belong to the class rather than to the object. They can be accessed from anywhere through the class. They are global variables of the class and are stored in memory when the class is created. For multiple objects, static data members are only stored in one place, which can save memory. As long as the value of the static data member is updated once, all objects are guaranteed to access the same updated value.
static method:
The method declared with static is a static method or a class method. When executing this method, the object reference will not be passed to the function, so we cannot access non-static members, only static methods or static variables. Only methods related to classes such as self static parent, etc. can be used. It can be executed without generating objects when using it
$this self:: parent::
$this
is a reference to the current object. It generally appears in methods and is used to obtain member attributes of the class or execute member methods of the class
self ::
Reference to this class, used to obtain the stated member attributes or static member methods of the current class self::run()
parent ::
Reference to the parent class, calling methods or properties of the parent class.
_______________________________________________________________

Construction method & Destruction method

Constructor method__construct()
It is automatically executed when creating an object and has no return value. It is used to perform some initialization work of the class, such as the initialization work of object attributes. The constructor method in PHP4 must have the same name as the class. In PHP5, the constructor method is __construct(). The advantage is It is not affected by the class name. If __construct does not exist, PHP will search for a method with the same name as the class and execute it automatically.
You can pass parameters in the constructor to define attributes. When both the parent class and the subclass define constructors, the constructor of the subclass will be executed
Destructor method__destruct():
Method used to be automatically executed when the object is destroyed in memory, without any parameters
_______________________________________________________________

Object final const

  1. <span class="com">//The methods in the class cannot be modified</span>
  2. <span class="com">//Class cannot be inherited</span>
  3. <span class="pln"> <span class="kwd">final<span class="pln"> <span class="kwd">class<span class="pln"> souji <span class="pun">{</span></span></span></span></span></span>
  4. <span class="pln"> <span class="kwd">final<span class="pln"> <span class="kwd">public<span class="pln"> $pinpai<span class="pun">;<span class="pln"> <span class="com">//Error reporting</span></span></span></span></span></span></span></span>
  5. <span class="pln"> <span class="kwd">final<span class="pln"> <span class="kwd">function<span class="pln"> congdian<span class="pun">()<span class="pln"> <span class="pun">{<span class="pln"> <span class="com">//Invalid</span></span></span></span></span></span></span></span></span></span>
  6. <span class="pln"> echo $this<span class="pun">-><span class="pln">pinpai <span class="pun">.<span class="pln"> <span class="str">"The phone is charged by 10v voltage"<span class="pun">;</span></span></span></span></span></span></span>
  7. <span class="pln"> <span class="pun">}</span></span>
  8. <span class="pln"> <span class="kwd">public<span class="pln"> <span class="kwd">function<span class="pln"> kaijidonghua<span class="pun">()<span class="pln"> <span class="pun">{</span></span></span></span></span></span></span></span>
  9. <span class="pln"> echo <span class="str">"<<< No boot animation >>>>"<span class="pun">;</span></span></span>
  10. <span class="pln"> <span class="pun">}</span></span>
  11. <span class="pun">}</span>
  12. <span class="kwd">class<span class="pln"> moto <span class="kwd">extends<span class="pln"> souji <span class="pun">{</span></span></span></span></span>
  13. <span class="pln"> <span class="kwd">public<span class="pln"> <span class="kwd">function<span class="pln"> __construct<span class="pun">()<span class="pln"> ​​<span class="pun">{</span></span></span></span></span></span></span></span>
  14. <span class="pln"> <span class="com">// $this->pinpai = "Motorola";</span></span>
  15. <span class="pln"> $this<span class="pun">-><span class="pln">congdian<span class="pun">();</span></span></span></span>
  16. <span class="pln"> <span class="pun">}</span></span>
  17. <span class="pln"> <span class="kwd">public<span class="pln"> <span class="kwd">function<span class="pln"> kaijidonghua<span class="pun">()<span class="pln"> <span class="pun">{</span></span></span></span></span></span></span></span>
  18. <span class="pln"> echo <span class="str">"Motorola trademark"<span class="pun">;</span></span></span>
  19. <span class="pln"> <span class="pun">}</span></span>
  20. <span class="pun">}</span>
  21. <span class="pln">$li <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> moto<span class="pun">();</span></span></span></span></span></span>
  22. <span class="pln">$li<span class="pun">-><span class="pln">kaijidonghua<span class="pun">();</span></span></span></span>
  23. <span class="com">//Define constants</span>
  24. <span class="pln">define<span class="pun">(<span class="str">"WEBNAME"<span class="pun">,<span class="pln"> <span class="str">"文豆"<span class="pun">);</span></span></span></span></span></span></span>
  25. <span class="kwd">function<span class="pln"> aa<span class="pun">(){</span></span></span>
  26. <span class="pln"> echo WEBNAME<span class="pun">.<span class="str">"Focus on PHPWEB development"<span class="pun">;</span></span></span></span>
  27. <span class="pun">}</span>
  28. <span class="pln">aa<span class="pun">();</span></span>
  29. <span class="kwd">class<span class="pln"> caiwu<span class="pun">{</span></span></span>
  30. <span class="pln"> <span class="kwd">const<span class="pln"> suilv<span class="pun">=<span class="lit">0.05<span class="pun">;</span></span></span></span></span></span>
  31. <span class="pln"> <span class="kwd">function<span class="pln"> __construct<span class="pun">(){</span></span></span></span>
  32. <span class="pln"> echo WEBNAME<span class="pun">;</span></span>
  33. <span class="pln"> <span class="pun">}</span></span>
  34. <span class="pln"> <span class="kwd">function<span class="pln"> kaigonzi<span class="pun">(<span class="pln">$xingming<span class="pun">,<span class="pln">$gongzi<span class="pun">){</span></span></span></span></span></span></span></span>
  35. <span class="pln"> $gongzi <span class="pun">=<span class="pln"> $gongzi<span class="pun">-<span class="pln">$gongzi<span class="pun">*<span class="kwd">self<span class="pun">::<span class="pln">suilv<span class="pun">;</span></span></span></span></span></span></span></span></span></span>
  36. <span class="pln"> <span class="kwd">return<span class="pln"> $xingming<span class="pun">.<span class="str">"The salary is: "<span class="pun">.<span class="pln">$gongzi<span class="pun">.<span class="str">"yuan"<span class="pun">;</span></span></span></span></span></span></span></span></span></span>
  37. <span class="pln"> <span class="pun">}</span></span>
  38. <span class="pun">}</span>
  39. <span class="com">/*Employee category*/</span>
  40. <span class="kwd">class<span class="pln"> yuangong<span class="pun">{</span></span></span>
  41. <span class="pln"> <span class="kwd">private<span class="pln"> $xingming<span class="pun">;<span class="com">//name</span></span></span></span></span>
  42. <span class="pln"> <span class="kwd">private<span class="pln"> $gongzisu<span class="pun">;<span class="com">//Salary number</span></span></span></span></span>
  43. <span class="pln"> <span class="kwd">function<span class="pln"> __construct<span class="pun">(<span class="pln">$xingming<span class="pun">,<span class="pln">$gongzisu<span class="pun">){</span></span></span></span></span></span></span></span>
  44. <span class="pln"> $this<span class="pun">-><span class="pln">xingming <span class="pun">=<span class="pln"> $xingming<span class="pun">;</span></span></span></span></span></span>
  45. <span class="pln"> $this<span class="pun">-><span class="pln">gongzisu <span class="pun">=<span class="pln"> $gongzisu<span class="pun">;</span></span></span></span></span></span>
  46. <span class="pln"> <span class="pun">}</span></span>
  47. <span class="pln"> <span class="kwd">function<span class="pln"> kaizi<span class="pun">(){</span></span></span></span>
  48. <span class="pln"> $caiwu <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> caiwu<span class="pun">();</span></span></span></span></span></span>
  49. <span class="pln"> <span class="kwd">return<span class="pln"> $caiwu<span class="pun">-><span class="pln">kaigonzi<span class="pun">(<span class="pln">$this<span class="pun">-><span class="pln">xingming<span class="pun">,<span class="pln"> $this<span class="pun">-><span class="pln">gongzisu<span class="pun">); </span></span></span></span></span></span></span> </span></span></span></span></span> </span></span>
  50. }
  51. <span class="pln"><span class="pun"> </span></span>}
  52. <span class="pun"></span>$lisi
  53. =
  54. new<span class="pln"> yuangong<span class="pun">(<span class="pln">"李思"<span class="kwd">,<span class="pln"> <span class="pun">"6000"<span class="str">);<span class="pun"><span class="pln"><span class="str"><span class="pun"></span></span></span></span></span></span></span> </span></span> </span></span>echo $lisi
  55. ->
  56. kaizi();<span class="pln"><span class="pun"><span class="pln"><span class="pun"></span></span> </span></span>
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 Recommendations
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!