Php learning notes object-oriented, php learning notes _ PHP Tutorial

Source: Internet
Author: User
Php learning notes: Object-Oriented, php learning notes. Php learning notes object-oriented, php learning notes public: This class, subclass, external objects can call protected: This class subclass, can be executed, external object: php learning notes: Object-Oriented and php learning Notes

Public: This class, subclass, and external objects can all be called.
Protected: This class subclass can be executed. external objects cannot be called.
Private: this class can only be executed, and neither the subclass nor the external object can be called.
Three main features of object-oriented programming

1) sealing

Closed, also known as information hiding. It is to separate the use and implementation of a class, and only keep limited interfaces (methods) with external connections. Developers who use this class only need to know how to use this class, instead of worrying about how this class is implemented. This allows developers to focus more on other things and avoid the inconvenience caused by the dependency between programs.

2) inheritance

Inheritance means that a derived class (subclass) automatically inherits the attributes and methods of one or more base classes (parent classes) and can override or add new attributes and methods. Inheriting this feature simplifies the creation of objects and classes and increases the code reusability. Inheritance is divided into single inheritance and multi-inheritance. PHP supports single inheritance, that is, a subclass has only one parent class.

3) polymorphism

Polymorphism refers to different objects of the same class. different results can be obtained using the same method. this technique is called polymorphism. Polymorphism enhances software flexibility and reusability.

Class Definition

A class can contain its own constants, variables (called "attributes"), and functions (called "methods ").
Like many object-oriented languages, PHP also uses class keywords and class names to define classes. The format of the class is as follows:

The code is as follows:


<? Php
Class myobject {
//......
}
?>

Definition: Aggregates objects with similar characteristics into a class, and defines the same attributes and methods of these similar objects. A class is a description of a similar object, called a class definition. it is a blueprint or prototype of the class object.

The class object is called an instance of the class. To create an instance of a class, you must use the new keyword.
001ZpQGYty6MeYnSNUh25 & 690

The code is as follows:


<? Php
// The class definition starts with the key word class. the class name is usually capitalized with the first letter of each word.
Class NbaPlayer {
Public $ name = "Jordan"; // define attributes
Public $ height = "198 ";
Public $ team = "Bull ";
Public $ playerNumber = "23 ";

// Define the method
Public function run (){
Echo "Running \ n ";
}
Public function dribblr (){
Echo "Dribbling \ n ";
}
Public function pass (){
Echo "Passing \ n ";
}
}
// Class-to-object instantiation
// When the class is instantiated as an object, the keyword "new" is used, followed by the class name and a pair of parentheses.
$ Jordan = new NbaPlayer ();

// Attribute members in the object can be accessed through the "->" symbol
Echo $ jordan-> name. "\ n ";

// The member methods in the object can be accessed through the "->" symbol
$ Jordan-> dribble ();
$ Jordan-> run ();
?>

Member method

Functions in a class are called member methods. The only difference between a function and a member method is that a function implements an independent function, and a member method is an action in the implementation class and is part of the class.
Next we will expand the above myobject class and add a member method for it. the code is as follows:

The code is as follows:


<? Php
Classmyobject {
Function getobjectname ($ name ){
Echo "product name:". $ name;
}
}
?>

The function of this method is to output the product name, which is passed in through the parameters of the method.
A class is an abstract description and a set of objects with similar functions. If you want to use methods and variables in the class, you must first put them into an object, that is, an object.

Class constant
Since there are variables, of course there will be constants. A constant is a constant value that does not change. A well-known constant is the circumference rate Pi. Use the keyword const to define constants, for example:
ConstPI = 3.14159;

Constructor

PHP 5 allows developers to define a method in a class as a constructor. Classes with constructors call this method each time a new object is created, so it is very suitable for initialization before using the object.

The code is as follows:


<? Php
// The class definition starts with the key word class. the class name is usually capitalized with the first letter of each word.
Class NbaPlayer {
Public $ name = "Jordan"; // define attributes
Public $ height = "198 ";
Public $ team = "Bull ";
Public $ playerNumber = "23 ";

// Constructor, automatically called when the object is instantiated
Function _ construct ($ name, $ height, $ weight, $ team ){
Echo "It is an NbaPlayer constructor \ n ";
$ This-> name = $ name;
// $ This is a pseudo variable in PHP, indicating the object itself. You can use $ this-> to access the attributes and methods of an object.
$ This-> height = $ height;
$ This-> weight = $ weight;
$ This-> team = $ team;
}

// Define the method
Public function run (){
Echo "Running \ n ";
}
Public function dribblr (){
Echo "Dribbling \ n ";
}
Public function pass (){
Echo "Passing \ n ";
}
}
// Class-to-object instantiation
// When the class is instantiated as an object, the keyword "new" is used, followed by the class name and a pair of parentheses.
$ Jordan = new NbaPlayer ("Jordan", "198", "98 kg", "Bull ");

// Attribute members in the object can be accessed through the "->" symbol
Echo $ jordan-> name. "\ n ";

// The member methods in the object can be accessed through the "->" symbol
$ Jordan-> dribble ();
$ Jordan-> run ();

// Every time an object is instantiated with new, the constructor will be called with the parameter list after the class name
$ James = new NbaPlayer ("James", "203", "120", "Heat ")
Echo $ james-> name. "\ n ";
?>

Destructor

The code is as follows:


<? Php
// The class definition starts with the key word class. the class name is usually capitalized with the first letter of each word.
Class NbaPlayer {
Public $ name = "Jordan"; // define attributes
Public $ height = "198 ";
Public $ team = "Bull ";
Public $ playerNumber = "23 ";

// Constructor, automatically called when the object is instantiated
Function _ construct ($ name, $ height, $ weight, $ team ){
Echo "It is an NbaPlayer constructor \ n ";
$ This-> name = $ name;
// $ This is a pseudo variable in PHP, indicating the object itself. You can use $ this-> to access the attributes and methods of an object.
$ This-> height = $ height;
$ This-> weight = $ weight;
$ This-> team = $ team;
}

// Destructor, which is automatically called at the end of program execution
// Destructor are usually used to clear resources used by programs. For example, if the program uses a printer, you can release the printer resources in the destructor.
Function _ destruct (){
Echo "Destroying". $ this-> name. "\ n ";
}

// Define the method
Public function run (){
Echo "Running \ n ";
}
Public function dribblr (){
Echo "Dribbling \ n ";
}
Public function pass (){
Echo "Passing \ n ";
}
}
// Class-to-object instantiation
// When the class is instantiated as an object, the keyword "new" is used, followed by the class name and a pair of parentheses.
$ Jordan = new NbaPlayer ("Jordan", "198", "98 kg", "Bull ");

// Attribute members in the object can be accessed through the "->" symbol
Echo $ jordan-> name. "\ n ";

// The member methods in the object can be accessed through the "->" symbol
$ Jordan-> dribble ();
$ Jordan-> run ();

// Every time an object is instantiated with new, the constructor will be called with the parameter list after the class name
$ James = new NbaPlayer ("James", "203", "120", "Heat ")
Echo $ james-> name. "\ n ";

// Call the destructor by setting the variable to null
// The destructor is triggered when the object is no longer used.
$ James = null;
Echo "from now on James will not be used. \ n"
?>

The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.

Object reference

The code is as follows:


<? Php
// The class definition starts with the key word class. the class name is usually capitalized with the first letter of each word.
Class NbaPlayer {
Public $ name = "Jordan"; // define attributes
Public $ height = "198 ";
Public $ team = "Bull ";
Public $ playerNumber = "23 ";

// Constructor, automatically called when the object is instantiated
Function _ construct ($ name, $ height, $ weight, $ team ){
Echo "It is an NbaPlayer constructor \ n ";
$ This-> name = $ name;
// $ This is a pseudo variable in PHP, indicating the object itself. You can use $ this-> to access the attributes and methods of an object.
$ This-> height = $ height;
$ This-> weight = $ weight;
$ This-> team = $ team;
}

// Destructor, which is automatically called at the end of program execution
// Destructor are usually used to clear resources used by programs. For example, if the program uses a printer, you can release the printer resources in the destructor.
Function _ destruct (){
Echo "Destroying". $ this-> name. "\ n ";
}

// Define the method
Public function run (){
Echo "Running \ n ";
}
Public function dribblr (){
Echo "Dribbling \ n ";
}
Public function pass (){
Echo "Passing \ n ";
}
}
// Class-to-object instantiation
// When the class is instantiated as an object, the keyword "new" is used, followed by the class name and a pair of parentheses.
$ Jordan = new NbaPlayer ("Jordan", "198", "98 kg", "Bull ");

// Attribute members in the object can be accessed through the "->" symbol
Echo $ jordan-> name. "\ n ";

// The member methods in the object can be accessed through the "->" symbol
$ Jordan-> dribble ();
$ Jordan-> run ();

// Every time an object is instantiated with new, the constructor will be called with the parameter list after the class name
$ James = new NbaPlayer ("James", "203", "120", "Heat ")
Echo $ james-> name. "\ n ";

// Object reference is used to access object attributes and methods. $ james, $ james1 and $ james2 are both object references.
// $ James and $ james1 are two independent references to an object.
// $ James2 is the shadow of $ james. Using the same reference of an object, if any value is null, the same reference is deleted.
$ James1 = $ james;
$ James2 = & $ james

$ James = null;
Echo "from now on James will not be used. \ n"
?>

Protected public: This class, subclass, and external object can call protected: This class subclass, can be executed, external object...

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.