Summary of "The difference between object-oriented class access and object access" This and class access ", access differences for static members, class constants, etc., inheritance and overrides, access modifier qualifiers, bubbling sort"

Source: Internet
Author: User

Object-Oriented Summary
1. First, object-oriented programming uses objects as a processing object "such as inheritance, and so on, the nature of inheritance, is still targeted at the object"
But only for class access, static members, and access modifier qualifiers, the concept of a class

2. When defined within a class, there are only five scenarios:
CLASS constant "const keyword definition and use class to invoke"
Property "These properties and methods are called for objects of that class instance."
The method "uses $this in this case to refer to the environment (the object that invokes the method), and only the object of the subclass instance is passed down to the static call in the inheritance."
A static property is a data that is shared between all objects of the class that are used as instances. Save in class "
Static methods


3. Objects
The initialization of the properties in the object is done by the constructor, and the method is defined in the constructor as few as possible, and the call is made using the external method of the constructor call if it must be used. "It's better to expand"


4. About the difference between static and non-static members
The main performance is in the use of static and non-static properties.
The static keyword is defined

Call differences for static and non-static members:
A static member corresponds to a class call "::" "The call used here has a self/parent and a class name to invoke"
A non-static member corresponds to an object invocation of an object called "," which calls itself using object names outside of the class, and calls within the class using $this to refer to "better encapsulation and independence"

Because of the fuzziness of static and non-static methods, it is possible to mix them with each other "but this is not recommended"
"The difference between a non-static method and a static method is not obvious, because the method itself is in the coded area, and even if the object calls a non-static method and does not create the zone alone, the difference between a non-static method and a static method is that the reference to the $this is used in the method, although, of course, can also be used to refer to a method in static access (used for environment downward transfer, although this usage is seldom used) "


5. Inherit "Extends"
Inheritance itself is an object concept in PHP implementation level. "is equivalent to an inheritance chain. This is more similar to JS. "
"The inheritance chain itself is the concept of an object, but because PHP needs to use classes to implement the syntax of the object, it behaves as a class inheritance"


Override mechanism in inheritance "only the private parent class will not be overridden by the quilt class, but inherits the two private attributes in the subclass"
The rewrite here is also to find the value of the corresponding property on the inheritance chain. "This and JS consistent" "can guarantee the sub-object personality, but also to ensure that the child object overrides the original definition of the parent class does not conflict, and does not affect the"


6. Determination of $this
(1) The $this used in a real call to a non-static method refers to the object that called the static method
(2) Occurs between two classes, using another class name in one class to invoke the method on the class "non-static method, because we assume that the reality of using $this refers to the situation will only occur in non-static method calls" "In this case the $this refers to the subclass of the object"
"The existence of an inheritance chain to determine the $this environment of this invocation scenario"


7. Accessing the Modifier qualifier
It is important to note that in the context of accessing the modifier qualifier, it is important to note that this concept is scoped to the class level.
"Because the access modifier qualifier itself refers to access to the members defined within the class, it is only available on the class because the class is the syntax of the design object, and this restriction is only set at the class level to be effective"

The access limit here refers to:
In-Class
Inheritance chain on the inside of the class
Out-of-class
And where to judge is where access refers to the location of the code "because the code access location is the basis for the encapsulation situation"

For example:
<?php
class A {
Public Function In_a_func () {
Var_dump ($this->property);//In Class A, in the parent class (inheritance chain) access
}
}
Class B extends A {
//member definition in class B!
//public $property = ' public property<br> ';
//protected $property = ' protected property<br> ';
Private $property = ' private property<br> ';
Public Function In_b_func () {
Var_dump ($this->property);//Access in Class B, access in this class
}
}
Class C extends B {
Public Function In_c_func () {
Var_dump ($this->property);//Access in Class C, Access
}
on subclass (inheritance chain)
$c = new C;
Var_dump ($c->property);//access to this location first to find the property attribute in Class C, but not found, corresponding to find the properties in Class B, but Class B Chinese private, so can not be displayed. Null
//$c->in_c_func ();//Access private $property
//$c->in_b_func () within the subclass of the inheritance chain;//access Private in this class $property
//$c->in_a_func ();//access private $property within the parent class on the inheritance chain

The above principle is to tell us that because it is the property value of private, it is only accessible in class B, that is, after the In_b_func method is called, the property attribute can be accessed. None of the other things. "This is the only way to limit the best."
"Because the inheritance relationship between the three classes above determines the ability of private to restrict calls to the $property property when using $c object access. As long as the public or protected can be accessed to the "


The question about the access of this property is not related to which object $this is pointing to "this is why the access modifier qualifier is set on the class"

"Summary: Access modifier Qualifier in case of inheritance"
So just pay attention to the problem that the private property cannot be overridden, as well as the access restrictions of the private property. "is to ensure that access within the class is normally accessed, and that the object environment is not related"
In reality, private does not use much.
The use of public and protected properties only requires attention to the restriction of access by the inheritance chain and the override mechanism. That is, the value of the property that is searched for first, and then the attribute is searched layer by level, which is the problem that needs attention.
"The overriding mechanism created by the inheritance chain is to ensure the priority of access and to ensure the personalization of subclasses"
"It is also important to note that in the overriding mechanism of inheritance, a property cannot be lower than the parent's original property (that is, if the parent class uses public, the subclass can only be qualified with public)"


"There's another bubble sort: each round compares two adjacent numbers, each finding a maximum (small) number, and putting it on the right (left)." To carry out the n-1 wheel implementation "
The algorithm is very simple, double nested implementation.

Summary of "The difference between object-oriented class access and object access" This and class access ", access differences for static members, class constants, etc., inheritance and overrides, access modifier qualifiers, bubbling sort"

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.