Php getting started

Source: Internet
Author: User
Tags autoload
Objects in php include a lot of content, such as the most commonly used php classes, interfaces, polymorphism magic methods (: _ construct (), _ destruct (), _ clone), and so on. the declared instance code of the class is as follows :? Php permission... objects in php include a lot of content, such as the most commonly used php classes, interfaces, polymorphism magic methods (: _ construct (), _ destruct (), _ clone), and so on.

The declared instance code of the class is as follows:

 

Member attributes:

The variables directly declared in the class are called member attributes/Variables. they can be of the scalar type and compound type in php, and the resource type and null type are invalid.

In addition, when declaring a member attribute, you must have a keyword to modify it: public, protected, private; no specific significance is required: var. when declaring a member attribute, there is no need to assign an initial value.

Member constants:

Modified with a const constant, for example, const PI = 3.1415926;

Constant output does not need to be instantiated. it can be directly called by class name + constant name. The format is: class name: constant name

Ps. special access methods: -------- "$ this" and "::"

1) $ "this" exists in each member method, which is a special object to use. the member method belongs to that object, and the $ this application represents that object. its function is to complete access between members of the object.

2) ":" becomes the scope operator. with this operator, you can call constants, variables, and methods in the class without creating objects. the syntax format is as follows:

Keywords: variable name/constant name/method name

Keyword: parent, which can call the member variables, member methods, and constants in the parent class member;

Self, which can call static members and constants in the current class;

Class name, which can call constants, variables, and methods in the class;

Member method:

A function declared in a class becomes a member method. multiple functions can be declared in a class, that is, an object can have multiple member methods. the declaration of the member method is the same as that of the function. the only special feature is that the member method can be modified with keywords to control its access permissions.

Class instantiation

Creation object:

$ Variable name = new class name ([parameter]); // class instantiation.

Member class:

$ Variable name-> member attribute = value;

Constructor and constructor

The constructor is the first method automatically called by the object after the object is created. it exists in the declaration of each class and is a special member method, which is generally used to complete some initialization operations. if no constructor exists in the class, a constructor without parameters is automatically generated by default.


The instance code is as follows:

Function _ construct (parameter list) {// method body };

The Destructor is the opposite of the constructor. it is the last method called before the object is destroyed. it completes a specific operation, such as closing the file and releasing the memory.

The instance code is as follows:

Function _ destruct () {// method body };

Object-oriented features: encapsulation, abstraction, and polymorphism.

Encapsulation:

Combine the member attributes and methods in the class into an independent unit, and hide the content details of the object as much as possible. the purpose is to prevent external errors from affecting internal data by ensuring that the internal data (member attributes and member methods) of the class cannot be accessed at will.

Class encapsulation is implemented through the public, private, protected, static, and final keywords. for the role of each keyword, see the relevant php documentation.

Inheritance:

Make a class inherit and have the member attributes and member methods of another existing class. the inherited class becomes the parent class, and the inherited class becomes the child class. inheritance can improve code reusability and maintainability. the extends keyword is used for class inheritance.

The instance code is as follows:

Class subclass name extends parent class name {// subclass method body .}

The parent: keyword can also be used to call the member method of the parent class in the subclass method. the format is as follows:

Parent: Member method (parameter) of the parent class );

Methods that overwrite the parent class:

The so-called method that overwrites the parent class, that is, the method inherited from the parent class is replaced by the method in the subclass, also called method rewriting. the key to rewriting is to create the same method as the parent class in the subclass. g includes the method name, parameter, and return type.

Polymorphism refers to the ability of a program to process multiple types of objects. php polymorphism can be implemented in two ways, namely, polymorphism through inheritance and polymorphism through interfaces.

Realize polymorphism through inheritance, that is, rewrite the inherited member method to achieve polymorphism.

The instance code is as follows:

  printMessage();    }else{ echo "error!";    } } $objectA=new SubClassA(); printMSG($objectA); $objectB=new SubClassB(); printMSG($objectB);

Implement polymorphism through interfaces, define interfaces, and empty methods. then class inherits interfaces.

The instance code is as follows:

   printMessage();  }else{ echo "error !";   } }  $objectA =new ClassA(); printMSG($objectA);  $objectB =new ClassB(); printMSG($objectB);

Abstract classes and interfaces.

Abstract classes and interfaces are special classes that cannot be instantiated. they can be used together with object-oriented polymorphism.

Abstract class:

An abstract class is a class that cannot be instantiated. it can only be used as the parent class of other classes. abstract classes are declared using abstract keywords. the format is as follows:

The instance code is as follows:

Abstract class name {abstract function Member method (parameter );//}

Abstract classes are similar to common classes, including member variables and member methods. the difference between the two is that an abstract class must contain at least one abstract method. abstract methods do not have a method body. the implementation of their functions can only be completed in sub-classes. abstract methods are also modified using the keyword abstract.

Interface:

The inheritance feature simplifies the creation of objects and classes and enhances code reusability. However, php only supports single inheritance. if you want to implement multiple inheritance, you must use interfaces.

Interface declaration: implemented by using the interface keyword. the methods declared in the interface must be abstract methods. variables cannot be declared in the interface and can only be declared as member attributes of constants using the const keyword, all the members in the interface must have the puclic access permission. the ainterface declaration interface format is as follows:

The instance code is as follows:

Inerface interface name {// constant member; // the member can only be a constant. // abstract method ;}

Because the interface cannot implement the instantiation operation, it can only be implemented in the form of the subclass inheritance interface. the implementation format is:

The instance code is as follows:

Class subclass name implements interface name 1 [, interface name 2, interface name 3,...] {// subclass method body .}

Common keywords:

1) final: final indicates the final and final. this means that the classes and methods modified by the final keyword are all final versions. it cannot be inherited or be a subclass. it cannot be rewritten or overwritten.

2) static: the member attributes and member methods modified with the static keyword are called static attributes and static methods. static member attributes and methods can be directly used without being instantiated.

Static attribute: it belongs to the class itself, but does not belong to any instance of the class. it is equivalent to a global variable stored in the class and can be accessed through the class anywhere. the access format is:

Class name: $ static property name;

If you want to access static attributes in the Member methods inside the class, add the operator "self:" Before the static attribute name.

Static method: because it is not restricted by any objects, you can directly reference static methods in the class without class instantiation. the reference format is as follows:

Class name: static method name (parameter );

If you want to call static methods in the Member methods inside the class, add the operator "self:" Before the static method name. in a static method, only static variables can be called, but normal variables cannot be called. in a normal method, static variables can be called.

In addition to non-instantiation, static members are used to retain the static data modified after the object is destroyed so that it can be called next time.

3) clone. object cloning can be achieved through keywords. the clone object has no relationship with the original object, that is, the cloned object will apply for a new bucket to store the original object content. the format is as follows:

$ Clone object = clone $ name of the original clone object;

After cloning, their n member methods, attributes, and values are exactly the same. if you want to re-initialize the copy, you need to use _ clone ().

Magic method _ clone () can re-initialize the cloned copy object. it does not need any parameters, and automatically contains $ this (copy object) and $ that (original object) object references.

Object comparison:

"=" Indicates comparing the content of two objects, "=" indicates comparing the reference addresses of two objects is equal.

Object Type detection: the instanceof operator can detect the object that the current object belongs.

Object-oriented --- Common magic methods:

The common magic methods we have learned above include _ construct (), _ destruct (), and _ clone. next we will introduce several common magic methods.

_ Get (), _ set ();

The preceding two methods are used to precisely copy private members or obtain values.

_ Set () sets a value for the private member attribute during the program running. it does not require any return value. the _ set () method includes two non-negligible parameters: variable name and variable value. this method does not need to be called proactively. you can add the prive keyword to the method money.

_ Get (): When the program is running, obtain the attribute value of the private member outside the object. it has a parameter: Private member attribute name. it returns a value that allows the object to be used externally. you are not allowed to call this method.

_ Isset (), _ unset ():

The isset () function is used to check whether a variable exists. in object-oriented systems, the isset () function can be used to detect public member attributes, but this function does not work for private member attributes. therefore, the _ isset () function is created for this purpose. the format is as follows:

Bool _ isset (string name );

_ Unset () is also used to delete the private member attributes of the specified variables and objects. the format is as follows:

Void _ unset (string name );//

_ Call ():

The _ call () method is used by php to call a non-existent or invisible member method () method to store the method name and its parameters (method name and method parameters ). the method parameters exist as arrays.

_ ToString () method:

The function is to convert an object into a string when echo or print is used to output the object.

If the _ toString () method is not available, a fatal error occurs when an object is directly output.

When outputting an object, note that the echo or print statement is followed by the object to be output directly. do not add any extra characters in the middle. otherwise, _ toSting () will not be executed.

_ Autoload () method:

Save an independent and complete class to a php page, and keep the file name consistent with the class name. this is a good habit for every developer. in this way, you can easily find it next time you use it. however, if you want to introduce many classes on a page, you need to use the include_once () function or the require_once () function to introduce them one by one. the _ autoload () method introduced in php5 can automatically instantiate the class to be used. when a class is not instantiated, _ autoload () automatically searches for files with the same class name under the specified path. if it is found, the execution continues; otherwise, an error is returned.

Address:

Reprinted at will, but please attach the article address :-)

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.