PHP Object-oriented

Source: Internet
Author: User

PHP Object-oriented

In object-oriented programming (English: object-oriented proargamming, abbreviation: OOP), an object is a whole that consists of information and a description of the information being processed.

Is the abstraction of the real world.

In the real world we are faced with objects such as computer TV bike lights.

The main three properties of the object are:

Object behavior: You can apply those actions to objects, turn on the lights, turn off the lights is the behavior.

The shape of the object: When the method being applied is how the object responds, the color dimension is out of shape.

For example, Animal (animal) is an abstract class, we can be specific to a dog and a sheep, and the dog and sheep is the specific object, they have a color attribute, can write,

Can run and wait for the behavior state.

Object-oriented content

Class-Defines the abstract nature of a thing. The definition of a class contains the form of the data and the manipulation of the data.

Object-is an instance of the class.

Member variables-variables defined inside a class that are not visible to the outside, but can be accessed through member functions, after the class is instantiated as an object, the variable

Can become an object's properties.

member functions-defined within a class that can be used to access the data of an object.

Inheritance-inheritance is the mechanism by which subclasses automatically share data structures and methods of the parent class, which is a relationship between classes. When defining and implementing a class, you can

On the basis of an already existing class, the content defined by the already existing class as its own content, and the addition of a number of new content.

Parent class-A class is inherited by another class, which can be called a parent class, or a base class, or a superclass.

Subclass-A class inherits other classes, called subclasses, or derived classes.

Polymorphic-polymorphism refers to the same operation or function, which can be used on multiple types of objects and obtain different results. Different objects, receive the same message

Can produce different results, a phenomenon known as polymorphism.

Overloading-Simply put, the function or method has the same name, but the argument list is not the same case, such a different parameter or method of the same name, called each other

overloaded functions or methods.

Abstraction-Abstraction is the abstraction of objects that have consistent data types (attributes) and behaviors (actions) into classes. A class is such an abstraction that he reflects an application-related

Important nature, while ignoring other unrelated content, any class division is subjective, but must be related to the specific application.

Encapsulation-encapsulation refers to the binding of the properties of an object in the real world to the behavior and placement within a logical unit.

Constructors-used primarily to create objects that are initialized, that is, to assign an initial value to an object member variable, always with the new operator in the statement that creates the object.

destructor-destructor (destructor), in contrast to constructors, when an object ends its life cycle (for example, the function where the object is already called), the system automatically

Perform destructors, which are often used to clean up the aftermath (for example, create a memory space with new when creating an object, and in a destructor before exiting

Release with delete).

PHP class definition

The PHP definition class is usually syntactically formatted as follows:

<? Php

Class phpclass{

var $var 1;

var $var 2 = "constant string";

function MyFunc ($arg 1, $arg 2) {

[...]

}

[...]

}

?>

The parsing is as follows:

class uses the class keyword followed by the name definition.

You can define variables and methods within a pair of curly braces ({}) after the class name.

A variable of a class is declared with Var, and a variable can initialize a value.

The function definition is similar to the definition of a PHP function, but the function can only be accessed through the class and its instantiated objects.

The variable $this represents its own object.

Php_eol is a newline character.

creating Objects in PHP

After a class is created, we can use the new operator to instantiate an object of that class:

$php = new Site;

$taobao = new Site;

$google = new Site;

The above code we created three objects, each of the three objects are independent, next we see how to access member methods and member variables.

Calling member Methods

After instantiating an object, we can use that object to invoke a member method that can only manipulate member variables of that object

PHP Constructors

Constructor, is a special method. Used primarily to initialize an object when an object is created, that is, to assign an initial value to an object member variable, which is always used with the new operator to

The statement that created the object.

PHP5 allows developers to define a method as a constructor in a class with the following syntax:

void __construct ([mixed $args [,,,,,]])

In the example above, we can initialize the $title variables by constructing the method:

function __construct ($par 1, $par 2)

{

$this->url = $par 1;

$this->title = $par 2;

}

Method overrides

If the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.

Access control

The access control of a property or method is implemented by PHP by adding the keyword public (publicly), protected (protected), or private.

Public: The members of a public class can be accessed from anywhere.

Protected (Protected): A protected class member can be accessed by itself and its subclasses and parent classes.

Private: A private class member can only be accessed by the class in which it is defined.

Access Control for properties

The class attribute must be positioned as public, protected, and private, and if defined with Var, is considered public.

Access control of the method

Interface

Using interfaces (interface), you can specify that a class must implement those methods, but you do not need to define the specifics of these methods.

Interfaces are defined by the interface keyword, just as a standard class is defined, but all of the methods defined in it are empty.

All methods defined in the interface must be public, which is the attribute of the interface.

To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, otherwise a fatal error is reported, and the class can implement multiple interfaces, separating the names of multiple interfaces with commas.

Constant

You can define a value that is always the same in a class as a constant. The $ symbol is not required when defining and using constants.

The value of a constant must be a fixed value, not a variable, a class property, a result of a mathematical operation, or a function call.

From PHP5.3.0, a variable can be used to dynamically invoke a class. However, the value of the variable cannot be a keyword (such as self parent or STATICF)

Abstract class

Any class that is declared abstract if there is at least one method inside it, then the class must be declared abstract.

A class that is defined as abstract cannot be instantiated.

A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation.

When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same as the parent class (or more lenient).

For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, not private, and the method

Must match, that is, the type and the required number of parameters must be the same, for example, the subclass defines an optional parameter, and the parent abstract method declaration does not have a yo,

There is no conflict between the two declarations.

Static keyword

Declares that a class property or method is static (static) and can be accessed directly without instantiating the class.

Static properties cannot be accessed through an object that a class has instantiated (but static methods can).

Because static methods do not need to be called through an object, the pseudo-variable $this is not available in a static method.

A static property cannot be accessed by an object through the operator.

Zi

PHP Object-oriented

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.