Php object-oriented development study NOTE _ PHP Tutorial

Source: Internet
Author: User
Php object-oriented development learning notes. Object-oriented programming (OOP) refers to a programming model and a programming method. Objects refer to the class set object-oriented programming (OOP), which refers to a program design model, it is also a method for program development. An object refers to a set of classes. It uses objects as the basic unit of a program and encapsulates programs and data to improve software reusability, flexibility, and scalability.

Note:
1. when defining object methods, although you do not need to write public as a public method by default, we recommend that you write it.

Php object-oriented Day 1

1. what is object-oriented?


Elements: abstraction, encapsulation, and sharing, emphasizing the object structure rather than the program structure.

What is a class?
It can be understood as a function set menu. We use classes to generate our methods.
Example: a computer
Class: Display/keyboard/host... set (encapsulation ).
Target: display, keyboard, host ...... One of them has its own unique functions.
Property: computer.
Methods: improve functions, watch movies, play games, program, and access the internet .......

2. use class to create a class
You can have a method only when you have a class.
Format:
Class method name {
......
}

Member attributes: custom variables (only one name ).
Member method: custom function.

3. class member attributes and member methods
Note: Do not use var to define member attributes using public.

Instance:

The code is as follows:
Class MyPc {// declare a class and define a method MyPc.
Public $ name; // member attributes.
Var $ price; // member attributes
Function vod () {// member method to return strings.
Return "test ";
}
}


4. use the new function object for instantiation
Format: new object name (parameter)
Instance 1:

The code is as follows:

Class MyPc {// declare a class and define a method MyPc.
Public $ name; // member attributes.
Var $ price; // member attributes
Function vod () {// member method to return strings.
Return "test ";
}
}

$ Pc1 = new Mypc (); // instantiate
$ Pc1-> name;


Instance 2:

The code is as follows:

Class MyPc {

Var $ key;
Public $ name;

Function vod (){
Echo "hello ";
}

}

$ Pc1 = new MyPc ();
$ Pc1-> key = "10101010 ";

Echo $ pc1-> key;
?>


Php object-oriented Day 2


1. create one or more objects.
2. the $ this keyword in the object.
3. initialize object _ construct ()
4. destructor _ destruct ()

-----------------------------------------

1. create one or more objects
To create one or more objects, we only need to use the new function multiple times to instantiate the class.
Instance:

The code is as follows:

Class MyPc {
Public $ name;
Public $ price;

Function vod (){
Return "playing movies ";
}
....
}

$ Pc1 = new MyPc ();
$ Pc2 = new MyPc ();
$ Pc3 = new MyPc ();

2. $ this keyword in the object
$ This keyword is a system variable used to access the object attributes and object methods of the current object.
Scope: This class.
Instance:

The code is as follows:
Class MyPc {
Public $ name ;--------
Public $ price; |
|
Function vod () {|
$ This-> name; <--------
}
....
$ This-> vod (); // The output calls public $ name;
....
}

Instance 2:

The code is as follows:

Class MyPc {
Public $ name;

Function vod (){
Return $ this-> name. "playing future policemen ";
}
Function game (){
Return $ this-> vod (). "running World of Warcraft ";
}

}
$ Pc1 = new MyPc ();
$ Pc2 = new MyPc ();

$ Pc1-> name = "Computer 1 ";
$ Pc2-> name = "Computer 2 ";
Echo $ pc1-> vod ()."
";
Echo $ pc2-> game ()."
";
?>


3. initialize object _ construct ()
Initialization is equivalent to pre-booking the initial values of a member attribute.

Format:
Class MyPc {
Function _ construct (){
Initialization
}
}

Instance:

The code is as follows:

Class MyPc {
Public $ name;

Function _ construct ($ name = "") {// initialization.
$ This-> name = $ name; // assign $ name = "" in the fourth row of the initialization content to the public $ name in the third row obtained by $ this ;.
}

Function vod (){
Return $ this-> name. "playing future policemen ";
}

Function game (){
Return $ this-> vod (). "running World of Warcraft ";
}

}
$ Pc1 = new MyPc ("Computer 1"); // initialize it here.
$ Pc2 = new MyPc ("Computer 2 ");

Echo $ pc1-> vod ()."
";
Echo $ pc2-> game ()."
";
?>

4. destructor _ destruct ()
A method that can be automatically called when an object is released and become an destructor. It can also be understood as a garbage collection mechanism.
Rule: The rule is followed by first-in-first-out, first instantiated and then released, and finally instantiated and called. The rule is released first.
Finally called.

Instance:

The code is as follows:

Class MyPc {
Public $ name;

Function _ construct ($ name = ""){
$ This-> name = $ name;
}

Function vod (){
Return $ this-> name. "playing future policemen ";
}

Function game (){
Return $ this-> vod (). "running World of Warcraft ";
}

Function _ destruct () {// first-in, first-out.
Echo"
Garbage collection mechanism: ". $ this-> name ."
";
}

}
$ Pc1 = new MyPc ("Computer 1 ");
$ Pc2 = new MyPc ("Computer 2 ");

Echo $ pc1-> vod ()."
";
// $ Pc1 = null; // here, if the memory needs to be recycled immediately after execution, null can be used.
Echo $ pc2-> game ()."
";
?>

Php object-oriented third day


Class encapsulation and application

1. encapsulation Keywords: public, protected, and private.
2. encapsulate related functions: __set (), _ get ().

-----------------------------------------------

1. encapsulation Keywords: public, protected, private
Encapsulation is to hide some related attributes and behaviors to ensure protection and security.

Encapsulate keywords
Public: Global, which can be accessed by external subclass inside the class.
Protected: indicates that it is protected and can be accessed only in this class or subclass or parent class.
Private: private, which can only be used within the class. [Important: when this keyword is used, calling a private property or method can only be called in this class. it feels like multiple processes, making a springboard. For details, see instance 2.

Public protected private
Global 1 0 0
Inheritance class 1 1 0
Class 1 1 1

Instance:

The code is as follows:

Class MyPc {
Public $ name; // public attributes
Protected $ price; // protection attribute

Private function vod () {// private attribute
$ This-> name;
}
...
}
?>

Instance 2:

The code is as follows:

Class MyPc {
Private $ name;
Function _ construct ($ name = ""){
Return $ this-> name = $ name;
}
Private function open (){
Return $ this-> name. "--- power on, starting ";
}
Public function OK (){
Return $ this-> open (). "--- OK ";
}
}
$ Pc = new MyPc ("My Computer ");
Echo $ pc-> OK ();
?>


Functions related to package: _ set (), _ get (): operation of private attributes or methods.
_ Set (): Gets the private attributes or private methods encapsulated in the current class, and re-executes or assigns values.
Format: __set ($ n, $ v)

_ Get (): gets (indirect access to prevent bare output) the encapsulated attributes or methods in the current class and converts them to public attributes.

_ Get/_ set instance:

The code is as follows:

Class MyPc {
Private $ name;
Public function _ construct ($ name = ""){
Return $ this-> name = $ name;
}

Public function _ get ($ name ){
Return $ this-> name. "_ get ";
}
Public function _ set ($ n, $ v) {// $ n corresponds to the object attribute $ name, $ v corresponds to the last and third rows _ set.
$ This-> $ n = $ v;
}
Private function open (){
Return $ this-> name. "--- power on, starting ";
}
Public function OK (){
Return $ this-> open (). "--- OK ";
}
}
$ Pc = new MyPc ("My Computer ");
$ Pc-> name = "_ set ";
Echo $ pc-> name;
?>

Class inheritance and application

1. Inheritance keyword: extends.
2. rules inherited by PHP.
3. base class method overloading and base class method access.

------------------------------------------------

1. Inheritance keyword: extends
The inheritance of PHP classes can be understood as the class capacity of shared inherited classes (base classes.
Note: PHP is a single inheritance.

2. Format

The code is as follows:

Class MyPc {
...
}

Class Home extends MyPc {
...
}

3. base class method overloading and base class method access
Format: base class name: Original base class method name

4. instance
Inheritance:

The code is as follows:

Class Root {
Public function print1 (){
Return "Root_print ";
}
}

Class Son extends Root {
Public function print2 (){
Return "Son_print ";
}
}

$ P = new Son ();
Echo $ p-> print1 ();
?>

Overload:
If a method of the base class needs to be enhanced in the derived class, you can use the overload function.

The code is as follows:

Class Root {
Public function print1 (){
Return "Root_print ";
}
}

Class Son extends Root {
Public function print1 (){
Return Root: print1 ()."
Son_print ";
}
}

$ P = new Son ();
Echo $ p-> print1 ();
?>


Class abstract methods and classes

It can also be understood as a standard. Define an abstract class and method at the beginning of the class, and then inherit the abstract class in the following class. in this way, you can forcibly name the methods of the following derived classes (just the abstract method name defined in the abstract class, you can add it on your own, but you cannot modify it ).

1. abstract Keywords: abstract.
2. definitions of abstract methods and abstract classes.
3. abstract class and method usage rules.

-----------------------------------------

1. abstract Keywords: abstract
Abstract means there is no definite explanation, but there are some concepts or names.

2. definition of abstract methods and abstract classes
At least one method of a class is abstract, which is called an abstract class.
Therefore, if an abstract class is defined, the abstract method is first defined.
Format:
Abstract class class1 {

Abstract function fun1 ();

...
}
Note: 1. the class must have at least one abstract method .; 2. abstract methods cannot have {}.; 3. abstract must be added before the abstract method.

3. abstract class and method usage rules
Abstract class features:
1. it cannot be instantiated and can only be inherited.
2. all abstract methods must be overloaded in the inherited derived classes before they can be instantiated.
Format (cannot be instantiated ):
Abstract class cl1 {

Abstract function fun1 ();

...
}

Format (instantiated ):
Class cl2 extends cl1 {

Function fun1 () {// overload the abstract method
}
...
}

---------------------------------------------------
Instance:

The code is as follows:

Abstract class ChouXiang {
Abstract function fun1 ();

}

Class PaiShengLei extends ChouXiang {
Public function fun1 () {// overload
Echo "test ";
}
}

$ P = new PaiShengLei ();
$ P-> fun1 ();
?>

Object-oriented programming (OOP) refers to a program design model and a method for program development. The object refers to a collection of classes...

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.