Oop object-oriented basics in PHP (i)

Source: Internet
Author: User

>>> you need to understand the following concepts
Object-oriented & process-oriented concepts:
   process oriented : Focus on the process of solving a problem. The greatest feature of process-oriented is the process of solving this problem by a function of one.
   Object-oriented : Focus on which object handles a problem. The most important feature of object-oriented is that it has a class with attributes and functions, and gets the object from the class to deal with the problem.


"Object Oriented"

1. What is a class?
A collection of a series of individuals with the same attributes (characteristics) and methods (behaviors), which are abstract concepts
2. What is an object?
From a class, an individual with a specific property value, called an object. The object is a specific individual.
For example: Humans have names, genders, and ages, but none of these attributes can have a specific value.
Zhang San, is one of the objects of mankind! Name Zhang San, gender male, age 23, after each attribute of the class is assigned a specific value, it becomes the Zhang San object.

3. What are the relationships between classes and objects?
A class is an abstraction of an Object! Object is the materialization of the class!
The class simply indicates which properties of such an object, but cannot have a specific value, so the class is abstract.
Whereas an object is to assign all properties of a class to a specific individual, all objects are specific.

Declaration and instantiation of a class:   

1. How to declare a class:

Class Name {

              Access modifier $ property [= default];
             [access modifier] Function method () {}
     }
     <  Br>2, declaring a class note: The
① class name can only have alphanumeric underline composition, the beginning cannot be a number, must conform to the large hump law;
the ② class name must be decorated with class, and there must be no () after the name
The ③ property must have an access modifier, and the method can have no access modifier;

3, instantiating objects and the invocation of object property methods?
  $ Object name = new class name ();//() can be called without

:
 $ Object name, property name; /use, when calling properties, property names cannot be with $
:
$this property name;

constructor function:
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, it is automatically invoked to assign an initial value to an object's properties!
3. What are the constructor functions?
① constructor name, must have the same name as the class
"Public" function person ($name) {
$this, name = $name;
}
② using the Magic method __construct
[Public] Function __construct ($name = "") {
$this, name = $name;
}

4. Considerations for the constructor function:

 ① the first notation, the 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 a constructor with parameters, there will be no more null parameter constructs, that is, the new person () cannot be used directly; The parameter list in the () after person must conform to the requirements of the constructor!!!
 ③ If two constructors exist at the same time, __construct will be used.


 5, destructor: __destruct ():
     ① destructor is called automatically before the object is destroyed!
     ② destructors cannot have any parameters
     ③ destructors are commonly used when objects are exhausted, freeing resources, closing resources, and so on!

6, Magic Method:
     php, give us a series of functions that begin with __, these functions do not need to be called manually, they will be called automatically at the right time, such functions are called magic functions.
    For example: function __construct () {} is automatically called when the class new object is
    function __destruct () {} is automatically called when the object is destroyed
& nbsp   We ask that, in addition to the Magic method, custom functions and methods cannot start with __.


    Finally, for more complex classes, we will write to a class file separately.
    class file naming, unified lowercase, using the "class name lowercase. class.php" way named.
    When you use this class in other files, you can import the. class.php file with include

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: 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 having to worry about the details of the feature implementation! (Encapsulation method)
② controls the user's data, prevents the setting of illegal data, and controls the data returned to the user (attribute encapsulation +set/get method)
3. Implementation of encapsulation operation?
Encapsulation of the ① method
For some methods that are only used inside the class, rather than being used externally. So, we can use private to do the privatization process.
Private Function FormatName () {}//This method can only be used within the class using $this call
function ShowName () {
$this-FormatName ();
}

Encapsulation +set/get method of the ② property to control the setting and reading of the property, you can privatize the property and ask the user to set it through the Set/get method we provide

Private $age;
function Setage ($age) {
$this->age = $age;
}
function Getage () {
return $this->age;
}
$ object, Getage ();
$ object, Setage (12);

Encapsulation of ③ attributes + Magic method
Private $age;
function __get ($key) {
return $this $key;
}
function __set ($key, $value) {
$this $key = $value;
}
$ Object->age; When accessing the private property of an object, the __get () Magic method is called automatically, and the Access property name is passed to the __get () method;
$ Object->age=12; When setting the object private property, the __set () Magic method is called automatically, and the property name and the property value of the set are passed to the __set () method;

Note: In the Magic method, you can use the branching structure 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 a parameter 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.
Use Isset () outside the >>> class to detect private properties, which are not detected by default. False
>>> So, we can use the __isset () function, which returns the internal detection results when automatically called.
function __isset ($key) {
Return Isset ($this-$key);
}
When externally using the Isset ($ object name-and private property), the results returned by the above __isset () are automatically invoked when detected!
④__unset ($key): The external use of the unset () function when the private property is deleted, automatically called;
function __unset ($key) {
Unset ($this, $key);
}
When external uses unset ($ object name-and private property), the property name is automatically passed to __unset () when the property is deleted, and is handled by this magic method.

To be continued "" "" "

Oop object-oriented basics in PHP (i)

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.