OOP in PHP

Source: Internet
Author: User
Tags modifiers

"Process-oriented and object-oriented"
* 1, process oriented: Focus on the process of solving a problem. The most important characteristic of the process is that a series of functions are solved to deal with the problem.
* 2, Object oriented: Focus on which object to handle a problem. The most important feature of object-oriented is the class that has the function of property and method, the object is taken from the class, and then the problem is dealt with.
*
* "Object Oriented"
* 1, what is a class?
* A collection of individuals with the same attributes (characteristics) and methods (behaviors). A class is an abstract concept.
*
* 2, what is an object?
* An individual with a specific attribute obtained from a class, called an object. The object is a specific individual.
*
* 3. What are the relationships between classes and objects?
* Class is the abstraction of an object, which is the materialization of a class.
The * class simply indicates what attributes such objects have. But there is no specific value, so the class is abstract.
* Whereas an object is to assign a value to all properties of a class, it produces a specific individual, so the object is specific.

"Declaration and instantiation of a class"
* 1, how to declare a class?
* Class Name {
* Access modifier $ property;
* [Access modifier] Function method () {};
* }
*
* 2. Notice of a class?
*① class name can only be composed of alphanumeric underline, the beginning can not be a number, must conform to the law of large hump;
The *② class name must be modified using class, and must not have () after the class name;
The *③ property must have an access modifier, and the method can have no access modifier;
*
* 3. Invocation of instantiated objects and object property methods:
* $ object Name =new class name ();//() can be carried without;
*
* Class external call properties and methods:
* $ object Name, property name,//property name cannot be used with the $ symbol when calling properties with
*
* Class internal call properties and methods:
* $this property name;
*
*
* "Common modifiers"
* Public, protected, private, var ...
*
* "Constructors"
* 1, what is a constructor function?
* A constructor is a special function in a class that is equivalent to invoking the constructor of a class when we instantiate an object with the new keyword;
*
* 2. What is the function of the constructor?
* When instantiating an object, automatic invocation is used to assign an initial value to an object's attributes;
*
* 3. How to construct the constructor?
The *① constructor name must have the same name as the class:
* [public]function person () {
* $this->name= $name
* }
*② using the Magic method
*
* 4, the construction function notes:
*① first, constructor name, must have the same name as the Class!!!
*② if a class does not have a handwriting constructor, the system will have an empty parameter construct by default, so you can use the new person ();
* If we write into a constructor with parameters, we will no longer have null parameter constructs, that is, you cannot directly use the new person (); The parameter list in the () after person must conform to the requirements of the constructor!! (That is, the problem of formal parameter arguments);
*③ if both constructors exist at the same time, the Magic method __construct is used.
*
* 5, destructor: __destruct ():
The *① destructor is automatically called before the object is destroyed;
*② destructors cannot carry any parameters;
*③ destructors are often used to release resources, close resources, and so on after the objects are exhausted;
*
* 6, Magic Method:
* PHP provides us with a series of functions beginning with __, these functions do not need to be called manually, will be called automatically at the appropriate time, such functions are called Magic functions (methods);
* For example: function __construct () {} is automatically called when class new is an object;
* Function __destruct () {} is automatically called when the object is destroyed;
* We require that, in addition to the Magic method, custom functions and methods can not start;
*
*
* Finally, for classes with more complex functions, we will write to a class file separately.
* class file naming, unified lowercase, using the "class name. class.php" Way named.
* When using this class in other files, use the Include to import the. class.php file.

"Basic knowledge of inheritance"
1, how to achieve inheritance?
* Add extends keyword to subclass, let subclass inherit parent class;
* Eg:class Student extends person{}

2. Considerations for Implementing Inheritance:
 *① subclasses can only inherit non-private properties of the parent class, and
 *② subclasses inherit the parent class, which is equivalent to copy the properties and methods of the parent class to the subclass, which can be called directly using $this;
 * ③php can only be single-inherited and does not support a class inheriting multiple classes. That is, PHP does not allow multiple inheritance!!!
 *   However, a class can perform multiple layers of inheritance;  
  eg:class person{}
 *    class Chengnian extends person{}
 *    class Student extends chengnian{}// The student class has all the properties and methods of the person class and the Chengnian class, and the
 3, method overrides:
 * conditional ① Subclass inherits the parent class,
 * condition ② Subclass overrides the parent class already has the method;
 *
 * conforms to the above two conditions called method overrides. After overwriting, the method called by the subclass invokes the subclass's own method of the same name;
 *
 * Similarly, in addition to method overrides, subclasses can have properties with the same name as the parent class, property overrides,  
 *
 * If the subclass overrides the parent class method, how do I invoke the parent class's method in the subclass?
 * Parent:: Method name ();
 *
 * Therefore, when the subclass inherits the parent class, the first step in the subclass construction is called, first the parent class is constructed and the assignment is made;
 *   eg:function __construct ($ Name, $sex, $school) {
    parent::__construct ($name, $sex);
     $this->school= $school;
   }

"Basic concepts of encapsulation"
1. What is encapsulation?
By accessing modifiers, properties and methods in the class that do not require external access are privatized to implement access control.
>>> Note that access control is implemented, not access denied. That is, after we privatize the attribute, we need to provide a corresponding method to let the user handle the property through the methods we provide.

2, the role of encapsulation?
*① users only care about the functionality that the class can provide, without needing to be concerned with the details of the implementation of the feature (encapsulation method)!
*② controls the user's data. Prevent the setting of illegal data, control the data returned to the user (attribute encapsulation +set/get method)!

3. Implement encapsulation Operation:
Encapsulation of the *① method:
* For some methods that are only used internally within the class, and do not want to provide no trial. So, we can use private privatization to deal with such a method;
* Eg:private function Say1 () {
Return "My name {$this->name}, this year {$this->age} years old! </br> ";
}
function say () {
echo $this->say1 ();
}

Encapsulation +set/get method for the ② property:
 * in order to control the setting and reading of the property, the property can be privatized and required to be set by the Set/get method we provide;
 *    Eg:private $age;
    function setage ($age) {
    if ($age >=0&& $age <= {
      $this->age= $age;
    }else{
     error_log ("wrong age setting! ");
    }
   }
   function getage () {
    return  $this->age;
   }
    $ Object->getage ();
    $ object->setage (n);

Encapsulation + Magic method of the ③ property;
 *    eg:private $age;
 *    function __get ($key) {
     switch ($key) {
       case ' name ':
       return $this $key. " (This is the text added when __get reads!) ) </br> ";
       
      case ' age ':
        return $this $key. " (This is the text added when __get reads!) ) </br> ";
      
      default:
        return $this $key. " (This is the text added when __get reads!) ) </br> ";
       break;
     }
    
   }

function __set ($key, $value) {
if ($key = = "Name") {
$this $key = $value. " (This is the text on the __set setting!) ) </br> ";
}else{
$this $key = $value;
}
}

$ object->age;//automatically calls the __get () Magic method when accessing an object's private property, and passes the property name that is accessed to __get ()
Method
$ object->age=12;//automatically calls the __set () Magic method when setting the object's private property, and passes the Set property name and the property value to the __set () method;
*
* Note: In the Magic method, the branch structure can be used to determine the difference between $key and different operations.

4, about the Magic method of encapsulation:
*①__set ($key, $value): called automatically when assigning a value to a class private property, passing two arguments to a method when invoked: the property name and the property value to be set;
*
*②__get ($key): Automatically called when the class private property is read, passing two parameters to the method when invoked: the name of the property to be read;
*
*③__isset ($key): called automatically when a private property is detected externally using the Isset () function.
* >>> external detection of private properties, default is not detected. False.
* So we can use the __isset () function to return the internal detection results when automatically called;
Eg:function __isset ($name) {
Return Isset ($this-$name);
}
* When externally using Isset ($ object name-and private property), the results returned by the above __isset () are automatically invoked when detected;
*
*④__unset ($key): External Delete private property is automatically called;
* Eg:function __unset ($name) {
Unset ($this, $name);
}
* When external use unset ($ object Name---private property), when deleting an attribute, the property name is automatically passed to __unset (), and is handled by this magic method.

"keywords"
1. Final:
*①final decorated class, this class is the final class, cannot be inherited;
*②final modification method, this method is the final method, cannot be rewritten;
*③final cannot modify attributes;

2, Static:
*① can modify properties and methods, called static properties, static methods, or class properties, or Class methods, respectively.
*② static Properties, static methods, can only be called directly using the class name.
* Use "class Name:: $ static Property", "Class Name:: Static method ()"
Person:: $sex; Person::say ();
*③ static properties, methods, which are generated the first time the class is loaded. Before the object is produced;
*
*④ static methods, you cannot invoke a non-static property or method, a non-static method can invoke a static property or method, (because static properties and methods are generated when the class is loaded, and not the static property method at this time there is no instantiation of the birth!). )
*⑤ in a class, you can use the Self keyword to refer to the class name;
* Eg:class person{
static $sex = "male";
function __construct ($name, $sex) {
$this->name= $name;
Self:: $sex = $sex;
}
}
echo Person:: $sex;

⑥ static properties are shared. That is, new many objects are also shared with one property;

3, the const keyword;
* Declare constants in the class, you cannot use the Define () function! You must use the Const keyword.
* Similar to the Define () declaration, the Const keyword declaration cannot take $ and must all be capitalized.
* Constants once declared, cannot be changed. Called with Static, called with the class name; Person:: constant;

4. instanceof Operator:
* Detect an object, whether it is an instance of a class (including grandparents ' generation ...) ;
* Returns a Boolean value;

"Small Summary" of several special operators:
1. Can only connect string "". "" ;
2. + = When declaring an array, the correlation key and value; ["Key" = "value"];
3, the object (this new object) invokes the member property, the member method.
4,:: ① Use the parent keyword to invoke the method with the same name in the parents class; Parent::say ()
② uses the class name (and self) to invoke static properties static methods and constants in the class;

OOP in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.