The object-oriented _php Foundation of PHP Learning notes

Source: Internet
Author: User
Tags constant constructor inheritance

Public publicly owned: This class, subclasses, and external objects can all be called
Protected protected: This class subclass, which can be executed, cannot be invoked by external objects
Private: Only this class can be executed, subclasses and external objects are not callable
Three main features of object-oriented programming

1) Sealing

Closed, can also be called information hiding. is to separate the use and implementation of a class, leaving only a limited number of interfaces (methods) to be contacted externally. For developers who use this class, just know how to use the class, without having to worry about how the class is implemented. Doing so allows developers to better focus their energies on other things, while avoiding the inconvenience caused by the interdependencies between programs.

2) The Inheritance of

Inheritance is that derived classes (subclasses) automatically inherit properties and methods from one or more base classes (parent classes), and can override or add new properties and methods. Inheriting this feature simplifies the creation of objects and classes and increases the scalability of the code. Inheritance and multiple inheritance, PHP supports single inheritance, which means that a subclass has and only one parent class.

3 polymorphism

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

Definition of Class

A class can contain constants of its own, variables (called "Properties"), and functions (called "Methods").
Like many object-oriented languages, PHP defines classes by adding class names to them. The format of the class is as follows:

Copy Code code as follows:

<?php
Class myobject{
......
}
?>

Definition: Birds of a feather flock to a class with objects of similar characteristics that define the same properties and methods that these similar objects have. A class is a description of a similar object, called a definition of a class, that is the blueprint or prototype of that class object.

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

Copy Code code as follows:

<?php
The definition of a class begins with the keyword class, and the name of the class is usually capitalized with the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining attributes
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";

Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Instantiation of class to object
Class with the name of the class and a pair of parentheses after using the keyword new,new when instantiated as an object
$jordan = new Nbaplayer ();

Property members in an 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 Methods

A function in a class is called a member method. The only difference between a function and a member method is that the function implements a separate function, and the member method is a behavior in the implementation class that is part of the class.
Next, expand the MyObject class above, add a member method to it, the following code:

Copy Code code as follows:

<?php
classmyobject{
function Getobjectname ($name) {
echo "Product name is:". $name;
}
}
?>

The function of the method is to export the product name, which is passed through the parameter of the method.
A class is an abstract description and a collection of similar sets of objects that function similarly. If you want to use the methods and variables in the class, you must first implement it to an entity, that is, an object.

Class constants
Since there are variables, of course there will be constants. A constant is a quantity that doesn't change, it's a constant value. One of the well-known constants is Pi Pi. Define constants using the keyword const as:
constpi=3.14159;

Constructors

The PHP 5 allowable developer defines a method as a constructor in a class. Classes with constructors Call this method each time a new object is created, so it is ideal to do some initialization before working with the object.

Copy Code code as follows:

<?php
The definition of a class begins with the keyword class, and the name of the class is usually capitalized with the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining attributes
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";

       //constructors, which are automatically invoked when an object is instantiated
         function __construct ($name, $height, $weight, $team) {
             echo "It is an  nbaplayer constructor\n";
            $this->name = $name;
           //$this is a pseudo variable inside PHP that represents the object itself. You can access the properties and methods of an object in a $this-> way
            $this-> Height = $height;
            $this->weight = $weight;
            $this->team = $team;
       }

Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Instantiation of class to object
Class with the name of the class and a pair of parentheses after using the keyword new,new when instantiated as an object
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull");

Property members in an 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 ();

Each time an object is instantiated with new, the constructor is invoked with the argument list following the class name
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat")
echo $james->name. " \ n ";
?>

destructor

Copy Code code as follows:

<?php
The definition of a class begins with the keyword class, and the name of the class is usually capitalized with the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining attributes
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";

constructor, which is invoked automatically when an object is instantiated
function __construct ($name, $height, $weight, $team) {
echo "It is a nbaplayer constructor\n";
$this->name = $name;
$this is a pseudo variable inside PHP that represents the object itself. You can access the properties and methods of an object in a $this-> way
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
}

destructor, which is invoked automatically at the end of the execution of the program.
Destructors are typically used for the resources used by the cleanup program. For example, when a program uses a printer, you can then release the printer resources from the destructor.
function __destruct () {
echo "destroying". $this->name. " \ n ";
}

Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Instantiation of class to object
Class with the name of the class and a pair of parentheses after using the keyword new,new when instantiated as an object
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull");

Property members in an 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 ();

Each time an object is instantiated with new, the constructor is invoked with the argument list following the class name
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat")
echo $james->name. " \ n ";

You can trigger a call to a destructor by setting the variable to null
The destructor is triggered when the object is no longer in use
$james = null;
echo "from" and "James won't be used.\n"
?>

Destructors are executed when all references to an object are deleted or when an object is explicitly destroyed.

References to Objects

Copy Code code as follows:

<?php
The definition of a class begins with the keyword class, and the name of the class is usually capitalized with the first letter of each word
Class nbaplayer{
Public $name = "Jordan"; Defining attributes
Public $height = "198cm";
Public $team = "Bull";
Public $playerNumber = "23";

       //constructors, which are automatically invoked when an object is instantiated
         function __construct ($name, $height, $weight, $team) {
             echo "It is an  nbaplayer constructor\n";
            $this->name = $name;
           //$this is a pseudo variable inside PHP that represents the object itself. You can access the properties and methods of an object in a $this-> way
            $this-> Height = $height;
            $this->weight = $weight;
            $this->team = $team;
       }

       //destructor, which is automatically invoked at the end of the execution of the program
       A  //destructor is typically used by the resource used by the cleanup program. For example, when a program uses a printer, you can then release the printer resource in the destructor
        function __destruct () {
             echo "destroying". $this->name. " \ n ";
       }

Defining methods
Public Function run () {
echo "running\n";
}
Public Function Dribblr () {
echo "dribbling\n";
}
Public Function Pass () {
echo "passing\n";
}
}
Instantiation of class to object
Class with the name of the class and a pair of parentheses after using the keyword new,new when instantiated as an object
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull");

Property members in an 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 ();

Each time an object is instantiated with new, the constructor is invoked with the argument list following the class name
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat")
echo $james->name. " \ n ";

object is used to access the properties and methods of the object, $james, $james 1 and $james2 are all references to objects
$james and $james1 are two separate references to objects
$james 2 is the shadow of $james, using the same reference of the object, and any assignment to null is equivalent to deleting the same reference
$james 1 = $james;
$james 2 = & $james

$james = null;
echo "from" and "James won't be used.\n"
?>

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.