Object-oriented in PHP and PHP

Source: Internet
Author: User

Object-oriented in PHP and PHP
Access Control (visibility)

PHP access control includes public, protected, and private)

It is defined as a public class member that can be accessed anywhere.
A protected class member can be accessed by itself, its subclass, and its parent class.
A private class member can only be accessed by the class it defines.

Cannot be used to modifyclass

Class attributes cannot be omitted and must be defined as public, protected, or private.

If these keywords are not set for methods in the class, the method is public by default.

Objects of the same class can access each other's private and protected members even if they are not the same instance. This is because the specific implementation details of these objects are known.

Attribute

Use the-> (Object operator): $ this-> property (where property is the property name) method to access non-static properties. The static attribute is accessed using: (double colon): self: $ property.

$ This is a reference to a caller object.

Class constant

You can define a constant value in a class. You do not need to use the $ symbol when defining and using constants.

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

Use double Colon: To access

const constant = 'constant value';
Automatic loading class

Many developers create a PHP source file for each class definition when writing object-oriented applications. A major headache is that you have to write a long list of contained files (one file for each class) at the beginning of each script ).

In PHP 5, this is no longer needed. You can definespl_autoload_register()Function, which is automatically called when attempting to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.

<? Php // function _ autoload ($ class) {// include 'classes /'. $ class. '. class. php '; //} function my_autoloader ($ class) {include 'classes /'. $ class. '. class. php ';} spl_autoload_register ('My _ autoloader'); // or, an anonymous function spl_autoload_register (function ($ class) {include 'classes/'can be used since PHP 5.3.0 /'. $ class. '. class. php ';});?>
Constructor and destructor

Constructor and destructor are not secretly called by the engine. To execute the constructor and destructor of the parent class, you must explicitly call the constructor and destructor of the subclass.parent::__construct(); parent::__destruct()

Inheritance

Unless automatic loading is used, a class must be defined before use. If one class extends the other, the parent class must be declared before the subclass. This rule applies to classes that inherit other classes and interfaces.

Inherit and useextendsKeywords

Range Resolution operator (::)

The range resolution operator (also known as Paamayim nekudow.im) or, more simply, a colon, can be used to access static members and class constants.

Static keywords

If the declared class property or method is static, you can directly access it without instantiating the class. Static attributes cannot be accessed through an object that has been instantiated by a Class (but static methods are acceptable ).

Static attributes and methods are regarded as public

Abstract class

Use keywordsabstract

If at least one method in a class is declared as abstract, the class must be declared as abstract.

An abstract method only declares its call method (parameters) and cannot define its specific function implementation.

When inheriting an abstract class, the subclass must define all abstract methods in the parent class. In addition, the access control of these methods must be the same (or more loose) as in the parent class)

Interface

The interface uses the interface keyword

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

Constants can also be defined in the interface. The use of interface constants and class constants is identical, but they cannot be overwritten by quilt classes or subinterfaces.

To implement an interface, use the implements operator. Class must implement all methods defined in the interface, otherwise a fatal error is reported. Class can implement multiple interfaces, separated by commas.

The interface can also inherit the interface

Traits

Since PHP 5.4.0, PHP has implemented a method for code reuse, called traits.

<?phptrait ezcReflectionReturnInfo {    function getReturnType() { /*1*/ }    function getReturnDescription() { /*2*/ }}class ezcReflectionMethod extends ReflectionMethod {    use ezcReflectionReturnInfo;    /* ... */}class ezcReflectionFunction extends ReflectionFunction {    use ezcReflectionReturnInfo;    /* ... */}?>

Use commas to separate multiple trait entries.

<?phptrait Hello {    public function sayHello() {        echo 'Hello ';    }}trait World {    public function sayWorld() {        echo 'World';    }}class MyHelloWorld {    use Hello, World;    public function sayExclamationMark() {        echo '!';    }}?>
Magic

_ Construct ()
_ Destruct ()
_ Call ()
_ CallStatic ()
_ Get ()
_ Set ()
_ Isset ()
_ Unset ()
The _ sleep () serialize () function checks whether a magic method _ sleep () exists in the class (). If this method exists, it is called first before the serialization operation is executed.
_ Wakeup () unserialize () checks whether a _ wakeup () method exists. If yes, the _ wakeup method is called first.
_ ToString () is used to respond to a class when it is treated as a string.
_ Invoke () when an object is called by calling a function, the __invoke () method is automatically called.
_ Set_state () When var_export () is called to export a class, this static method will be called
_ Clone ()
_ DebugInfo () var_dump () is called when an object is created.

These functions are called Magic methods in PHP ). You cannot use these method names when naming your own class methods unless you want to use their magic functions.

When you assign a value to an inaccessible attribute, __set () is called.

When you read the value of an inaccessible attribute, __get () is called.

When isset () or empty () is called for an inaccessible attribute, __isset () is called.

When unset () is called for an inaccessible attribute, __unset () is called.

When an inaccessible method is called in an object, __call () is called.

When an inaccessible method is called in static mode, __callstatic () will be called.

Final keywords

If the method in the parent class is declared as final, the subclass cannot overwrite the method. If a class is declared as final, it cannot be inherited.

Attributes cannot be defined as final. Only classes and methods can be defined as final.

Object Replication

Object replication can be completed by using the clone keyword (if possible, the _ clone () method of the object will be called ). The _ clone () method in the object cannot be called directly.

Object comparison

When the comparison operator (=) is used to compare two object variables, the comparison principle is: if the attributes and attribute values of the two objects are equal, and the two objects are instances of the same class, the two object variables are equal.

If the full equality operator (=) is used, these two object variables must point to the same instance of a class (that is, the same object ).

Type Constraints

PHP 5 can use type constraints. Function parameters can be specified as objects (specify the class name in the function prototype), interfaces, arrays (from PHP 5.1) or callable (from PHP 5.4 ). However, if NULL is used as the default value of the parameter, NULL can still be used as the real parameter when calling the function.

Type constraints cannot be used for scalar types such as int or string. Traits is not allowed.

Type constraints can be used not only in member functions of a class, but also in functions.

Object serialization

All values in php can be expressed using the serialize () function to return a string containing byte streams. The unserialize () function can re-convert the string back to the original value of php. Serializing an object will save all the variables of the object, but will not save the object method.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.