Object-oriented is not a technology, but a kind of thinking, is a problem-solving the most basic way of thinking! How to understand usage? OOP: Object-Oriented Programming
(Direct code description)
1. Example of an object-oriented basic concept demo:
1<?PHP2 Header("Content-type:text/html;charset=utf-8");3 //How do I instantiate a class? For example: $newA = new A (); Using a class to get an object, the procedure is an instantiation of the class. The keyword new is complete.4 //How do I define a class? Example: Class a{} classes5 //How do I declare a property? For example: Public $age control modifiers for property declarations: 1.public 2.protected 3.private6 //How do I declare a definition method? For example: Public function User () {} Method Object member (property, method) accessor:->7 //How do I define Class constants? For example: const work = ' PHP '; Class constant Access Form: Class Name:: Constant name8 //Summary: Only three members in a class: 1. Property (variable) 2. Method (function) 3. Class constant (constant)9 classa{//classTen Public $name;//Properties One Public $age; A ConstWork = ' php ';//class Constants - Public functionUser$object){//Method - Echo' Name: '.$object->name. ' Age: '.$object->age. ' Work '. Self::work;//inside a class, use the keyword self instead of the current class name the } - } - $newA=NewA ();//Instantiate object ==> class a{} - $newA->name= ' Admin_a '; + $newA->age = 100; - $newA->user ($newA); + A at //How do I use $this? For example: $this represents the current object of the class that currently belongs, who is currently calling, who is the current object - //Summary: A property cannot be accessed directly within a method, it needs to find its object before it can be accessed, and a typical practice is to use the $this keyword within a method in place of the current object. - classb{ - Public $name; - Public $age; - ConstWork = ' html '; in Public functionUser () { - Echo' Name: '.$this->name. ' Age: '.$this->age. ' Work '. Self::work;//$this Current Object to } + } - Echo"<pre>"; the $newB=Newb (); * $newB->name= ' Admin_b '; $ $newB->age = 200;Panax Notoginseng $newB->user ($newB);
Running results such as:
Basic knowledge of object-oriented programming (OOP) in PHP