Object-oriented 1

Source: Internet
Author: User

the difference between object-oriented and process-oriented programming

V- oriented process flow is simple, suitable for some short-term small projects;

V Object-oriented modularity is deeper, security is stronger, data is more closed

V from the point of view of early development, object-oriented is more complex than process-oriented, but from the point of view of maintenance and upgrade, object-oriented is far simpler than the process-oriented!

Object-oriented syntax rules

Create a class :

Class name {}

Class properties

there are class properties within the class, syntax rules Example:

class name {

Public $ Property name ;

}

Property name is called by an object after instantiating an object

Object

Creation syntax and instantiation of objects

$ Object name =new class name;

object to invoke the syntax of the property:

$ Object name , property name (PS: no $)

Class method

The class method is the name of the method written in the class.

Grammar:

class name {

Public Function Method name () {}

}

keyword within a method $this

Object-oriented invocation methods are the same as calling properties

However, if I change the properties within the class by this method, the script does not know who called it, this time it needs to $this this keyword

$this: The object that is currently calling this method

Grammar:

Call Property or call method $this

Construction Method

A function method that automatically triggers a system when instantiating an object

function Name:__construct ()

If this method is a tangible parameter, where does the argument enter?

Cases:

$ Object name =new class name ( argument );

destructor Method

function Name:__destruct ()

when the object is going to disappear, the system automatically triggers the function , the object disappears generally have two kinds of cases, delete object and script end

Generally without parameters

the value of the object

Objects have value passing and reference passing as variables

However, the value of an object is passed in the same way as a reference pass, and the effect is the effect of a reference pass, so it is common to use a value to pass it.

It's the same as a variable.

Cloning of Objects

Grammar:

New object name =clone The name of the object to clone

Cloning an object does not trigger a construction method, because the constructor is triggered when an object is instantiated, but the clone is not an instantiated object and can be understood as copying

However, after cloning and the original object has a certain difference, the value is not the same as the property storage space

If you want a cloned object to have a construction method,

need to use magic method __clone

This magic method can be likened to the construction method of the cloned object, almost the same as the construction method

Static Members

Static properties

Grammar:

class Name::$ static property name

when calling a static property, there is a difference between the normal attribute and the property name, which needs to be added $

defining a static property requires a keyword static

Cases:

public static static property name

If you access a static member within a class, you can use the Self keyword instead of the class name

Static methods

static methods, like static properties, require static, which is the keyword

The difference between a static method and a non-static method is:

A non-static method can be said to be an object method, which is used by the object;

Static methods are called by the class;

class Constants

is defined in a class of constants, in fact, and ordinary constants are no different, just class constants need to find the class to access the constant!

Declaration of a class constant

using the const keyword

Unlike attributes, the declaration does not need to add $

Note: You do not need to add any access modifiers before class constants

To access the syntax of a class constant:

Class Name:: Constant Name

class file automatic loading mechanism

in general, our class files in the project are created in the format of the class name. class.php , because this is to facilitate automatic loading of our class files.

Function method:

__autoload ( class name ) all class-related operations will trigger this magic method

Then we can automatically load out the file of the desired class by obtaining the class name, for example:

Public Function __autoload ($class _name) {

Include './'. $class _name. Class.php ';

}

but this method can only be used once, if we appear in the loaded class file also has this automatic loading function h , then the system will error, so this time, we need to register their own automatic loading mechanism

registering the automatic loading mechanism

Function method:

Spl_autoload_register ( name of function );

You can write a number of methods, automatically load the file method, and then this method can be used to register other function methods as an automatic loading method, note, because the parameter is the function name, so it needs to be quoted!

Sequence number and inverse sequence of an object

Also known as the serialization of objects!

Object is also a data type, since it is the data, there is a need to be persisted storage problem!

Storage of Data

There are two types of media persisted in data persistence: file or Database!

To save to a file as an example:

Memories:

Write data to a file:

File_put_contents ( file address, data ), the return value is the length of the string written!

Read data from a file:

File_get_contents ( file address )

But if the content is written directly, no matter what type it is written, it will automatically turn into a string type, which violates the heart of the data we're storing.

So this time we need a way

data Serialization:Serialize ( data )

When you output the data, you need the deserialization session to output the data

method:unserialize ( data )

Object-oriented 1

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.