1.mysql Package Class
Define parameters for initializing database connections in a constructor "because the parameter passed in when instantiating an object is the only parameter entry for that object"
But instead of redundant definitions in the constructor, it is possible to set up the constructor by invoking other methods in the constructor, such as "module separation makes logic simpler"
Focus
2. Static members
Equivalent to a "count function" that is related to each instantiation of an object
Because the objects that are created each time the class is instantiated are newly created, it is important to note that the variables between objects under the same class have no interactive effect.
"Recall the use of static local variables in the original function"
"Deferred to a static global variable (without this concept), that is, the static property in the object"
So we need the data that's shared between the objects "static properties" is the basis of the implementation of the singleton, guaranteeing the single instance object of the database connection "
A static member of a class: A member that is shared by all instances of an object of that class, that is, a static member of the class. The principle of implementation is that only static members are stored in the class for tagging, and only in this way can the principle of sharing between objects be guaranteed "
"Need to declare with the static keyword"
"So what are the use scenarios for static methods?" The static method is equivalent to the count of the number of calls to the method, i.e. the implementation of a singleton requires a static method "
"This is the access control character"
"::" Static Accessor "class name:: Member name" "Class accessor (because the member is saved in the class so that the shared data of the object that controls the instance between the classes is guaranteed)" "Also called the scope resolution operator"
The object accessor "," as required (object name and member name) for access "
"keywords"
$this "An object in the class that is the instance of the class. So you can only use object access controls. can also be used in classes only "
Self "in the class table is the current class, only with the class access control. (can only be used in a class) "
Parent "This can only refer to the parent class. You can only use the class access control
Also notice that access control for the object or class is accessed outside of the class, using the class name or object name.
A clear understanding of the access control and keywords "is because the reference and applicable scenarios are inconsistent before there is such a need to differentiate, do not need rote, very good understanding"
"The emphasis is on the use of this in methods, and the difference between static and non-static principles."
3. Static usage Issues
Static members can only be accessed using static accessors.
But the only exception is PHP support: Object name:: Static member "is for access to static members." Because the object already has an instance of the value, it is also possible for the object to find a corresponding class operation. "
Class Name:: Non-static method name; "Can execute normally, but will error"
For example:
<?php
Class Student {
Public $name = ' itcast ';
public static $count = 10;
Public Function Non_static_func () {
Var_dump ($this);//This is the difference between static and non-static methods
Echo ' non-static method <br> ';
}
public static function Static_func () {
Var_dump ($this);
Echo ' static method <br> ';
}
}
$o = new Student;
$o->non_static_func ();
$o->static_func ();//
Student::static_func ();//
Student::non_static_func ();//
In all four cases, they can be accessed normally.
"The reason is that the method itself is a collection of executable code, which is why the method, even if it is static, can be executed normally, and it is consistent." So in the PHP mechanism, the method itself in the class is equivalent to static, the method is registered in the executable code area, each object points to the method (then the equivalent of a static method) "
That's the difference.
"Then the difference between static methods is that using $this in a static method must not be available. $this Use this to refer to the current object only if the object is instantiated and the non-static method is invoked using the object "
"But try not to apply the situation"
Under normal programming, we still use objects to access non-static members, using classes to access static members "so that the individual accessors are also easy to distinguish"
"At the business logic level, it is possible to differentiate whether it is a static method: The method uses a non-static method if it is a certain instance object. If the method applies to all objects, set the method to a static method. 】
Although a non-static method is also an object reference to the encoding area for execution, $this in the method can specify the object for operation instead of using the class. So using this method can reflect the particularity of some objects "
"Note the difference"
4. Class constants
const keyword
Defined within a class, belonging to the class "differs from Define,define can be defined in any location, used anywhere (as long as define executes)"
Class constants do not need to use access modifiers (keywords such as public) to access "but as long as the class can be accessed, the constant can be used"
Access-time and static properties consistent "class:: Constant Name" "Simply does not require access, because $ is the basis for distinguishing variables and constants"
"Summary: Focus"
There are only five types of members in the class:
Property
Method
Static properties
Static methods
Class constants
Using syntax such as echo directly in a class will cause errors directly (including statements that use assignments in the Class). Only these statements can be encapsulated in the appropriate methods to be used.
Focus
5. Inheritance
If an object uses a member from another object, it is called an inheritance. "Although inheritance is implemented at the class level, it is essentially inherited by the members of the object"
Extends keyword "The inherited member inherits from the object during the instance"
Principle:
The concepts in object-oriented are all on the object, but in syntax, classes are required to implement these concepts.
The meaning of appearance is object-oriented code reuse.
Inheriting subclasses and inheriting the parent class is a many-to-one relationship "but it is not possible for subclasses to inherit multiple parent classes." That is, PHP itself does not have the concept of multiple inheritance "
"The object that is instantiated is also an instance of the inheriting parent class instanceof to determine the object's instance control of the class"
So from the object level to consider inheritance, but the implementation of the grammar is at the level of the class. This is much simpler.
6. Override the "re-definition of inherited members and the parent keyword issue"
An inherited member is overridden because it is redefined by the same name. "But in fact is not a rewrite, but similar to the relationship of JS inheritance chain, a layer of search for the name of the property appears, if it appears, that the value is displayed"
"Such a lookup mechanism is the principle of a layer refinement that restricts the determination of the value of an object's properties." can also guarantee that the modification of a subclass has no effect on the parent class "
Parent is used to refer to inheriting parent class "is also used in inheritance, in order to rewrite can also be more free to call the inherited parent class members"
"You need to use class accessors. Methods that are used to access the parent class (these methods must have been overridden by a method of the same name as the quilt class, otherwise you do not need to use parent for access) "
7. Determination of $this
Because the invocation of an inherited parent exists, it is equivalent to a call to a method in the inherited parent class of a class
"It is possible that the $this keyword scenario is present in this method. This is the case where this is used when the class invokes the method "
"There is a problem with the case where only the object calls the non-static method when the $this can access it properly"
Access to $this
(1) Which object calls the method, and the $this in the method refers to that object
(2) The object environment can be passed down! "Is the case of inheritance" "As long as the object of the subclass has been determined, the method in the subclass determines the object environment, and within that method, the static call to the non-static method will pass the object environment to the non-static method. At this point the $this is the object of the subclass (although $this is defined in a non-static method in the parent class, and is called by the Quilt Class) "
So the key to $this is only two "real object invocation, or down pass" (the object specified in the subclass when calling the parent class non-static method is $this. However, static calls to non-static methods do not necessarily occur in an inherited scenario) "
Example:
<?php
Class A {
Public Function in_a () {//Here try to change the In_a method to a static method
Var_dump ($this);
}
}
Class B {
Public Function In_b () {
Var_dump ($this);
Echo ' <br> ';
A::in_a ();//When called at this time, $this refers to the $o object under the specified method in Class A "but the actual use of such calls is not much."
}
}
$o = new B;
$o->in_b ();
?>
$this is determined to be only two points, the 2nd is especially important "the characteristics of the downward passing, but when the actual construction class, try to avoid this situation (static call non-static method). If it must be called, the non-static method is constructed as a static method. Prevent the $this of the disorder "
8. Accessing the Modifier qualifier
Public common
Protected protected "This class and subclass can access"
Private privately owned
PHP uses the concept of classes to restrict access to members. "Object differs from class"
Limit within three ranges
In-Class
Out-of-class
Inheritance chain in the class "JS also has an inheritance chain"
The above three ranges include all access areas.
The
determines where the member is defined and where it is accessed to determine access restrictions. "Where the code is, it definitely doesn't mean that an object instance class can represent access within that class." Instead, it is accessed in an object called outside the class "
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 private
$c->in_c_func () outside the class, or access the private $property
$c within subclasses of the inheritance chain->in_b_ Func ();//Access private $property
$c->in_a_func () within this class;//Access private $property within the parent class on the inheritance chain
Var_dump ($c->property);//out-of-class access protected
$c->in_c_func ();//access protected within subclasses of the inheritance chain $property
$c->in_b_func ();//access protected $property within this class
$c->in_a_func ();//access protected within the parent class on the inheritance chain $property
Var_dump ($c->property);//access public outside the class
$c->in_c_func ();//access public within subclasses of the inheritance chain $property
$c->in_b_func ();//access public within this class $property
$c->in_a_func ();//access public within the parent class on the inheritance chain $property
These are all the differences between access locations and restrictions
"The key is also the access location of the method and the location of the property within the method is different, that is, even if the outside of the class (object) to access the method within the class, as long as the method within the class has access to the property, then this access is in-class access"
Focus The understanding of the location and limitations of access, in fact, is the difference between the code location of the object and the class, the access restrictions are completely done "
Focus
The purpose of such access modifiers is to encapsulate the module by restricting access, and only open interface "interfaces can be accessed, but can hide internal implementations"
Do not require external use of properties, methods for private or protection, and simply leave the interface for public access can
9. The issue of accessing the modifier qualifier
The main problem with accessing the scoping of modifier qualifiers is:
The location of the code defined and accessed is determined to be within the class, the inheritance chain, the outside of the class
"Even if the class inherits another class, the property also appears in the subclass object, but access to the property is on the inheritance chain." Because the role of the access modifier qualifier is the concept of a class, inheritance itself is implemented between objects and does not affect the scope of access adornments "
Attention:
(1) In the rewrite, you need to be aware of where the access is defined "because there is an inheritance chain problem, so the overridden member is the first member to be accessed, the restriction of this permission is to pay attention to"
(2) The overriding problem for private members "private members cannot be overridden" ""
Cases:
<?php
Class B {
Members are defined in class B!
Public $public _p = ' on B public ';
protected $protected _p = ' in B protected ';
Private $property = ' in B public property<br> ';
Public Function In_b_func () {
Var_dump ($this->property);//Access in class B, access within this class
}
}
Class C extends B {
Public $public _p = ' on C public ';
protected $protected _p = ' in C protected ';
Private $property = ' in C public property<br> ';
Public Function In_c_func () {
Var_dump ($this->property);//Access within Class C, access on subclass (inheritance chain)
}
}
$o = new C;
Var_dump ($o);//This returns an object that contains four properties "one of the two private properties" "private property cannot be overridden"
Echo ' $o->in_c_func ();
$o->in_b_func ();//The value accessed at this time differs from the previous
Focus
When working with private properties, be sure to define the location of the private property "because the private property is not overridden"
If a class requires inheritance, it does not use the Private keyword for restricted access. "This avoids the problem of inconsistent access results."
(3) Override the inherited member and modify its access modifier qualifier "The qualifier permission for inheriting subclasses cannot be lower than the permissions of the parent class"
"Otherwise it will be an error."
Cases:
<?php
Class A {
protected function F1 () {
Echo ' in A F1 ';
}
}
Class B extends A {
Public Function F1 () {
Echo ' in B F1 ';
}
}
Class C {
var $name;
Function F1 () {
}
}
Class D {
Public $name;
Public Function F1 () {
}
}
10. Bubble sort
The next two numbers are compared and then the specified sort "ascending to the back row"
The principle of bubble sorting is that each round of comparisons can fetch a maximum value of "or a minimum".
Each round needs to be compared to the rest of the elements
There are a total of n elements
1 rounds need to compare n-1 a comparison
2 Wheel N-2
...
N-1 1
The total number of comparisons is
(n-1) + (n-2) +...+1
is to select a number for each round to be placed at the last position specified, and then to compare the remaining elements.
<?php
$array = Array ();//array to bubble sort
for ($i =0; $i <count ($array); $i + +) {
for ($j =0; $j < (COUNT ($array)-$i); $j + +) {
if ($array [$j]> $array [$j +1]) {
$a = $array [$j];
$array [$j] = $array [$j +1];
$array [$j +1] = $a;
}
}
}
?>
The bubble sort is realized above
Problem
"We need a complete summary today."
Need to summarize the relationship of access modifier restrictions, inheritance, overrides, keywords, and accessors "classes and objects"
"The relationship between access restrictions and inheritance and overrides needs to be noted, as well as the relationship between the inheriting subclass and the parent class"
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