Object-oriented programming-core knowledge points-automatic loading-class constants-static members

Source: Internet
Author: User
Use the strpos () function to do this. if the return value of this function exists, otherwise false is returned. in this way, if the return value of this function is false, it indicates that there is no limit.

Use oop and mysql extension to complete a database operation class:

Create a new DB. class. php

Design attributes:

Design getRow ();

Optimize the select statement entered by the user. if the user's select statement does not contain the limited clause, we will manually add the limit 1 clause.

We need to determine whether the user's select statement has a limit. Determines whether limit exists in the string $ query?

Use the strpos () function to do this. if the return value of this function exists, otherwise false is returned. in this way, if the return value of this function is false, it indicates that there is no limit.

After completing the above code, we found that all database operations require the mysql_query () function, and usually the execution is successful. Therefore, the execution of SQL statements involves repetitive work. Therefore, repetitive code should be extracted.

You need to define a special method to execute SQL. if the execution fails, the script stops running and an error message is output.

Call this method where it is needed.

Separate class files

Normally, we write the class definition to a special file.

Class name. class. php format

To use this class, you need to introduce the class definition file into the current script.

Note: the class definition cannot be repeated. Therefore, when introducing an external class file, you must note that it cannot be repeated. otherwise ..

You can use require_once to solve the problem.

# P # object-oriented programming-automatic loading # e #

Automatic loading:

When most of our projects are written fully in accordance with the object-oriented thinking, it means that there will be many classes and many class files.

Class files must be introduced whenever an instance object is created.

Automatic loading is used to solve the problem of introducing class files every time:

All classes are loaded in a unified manner, instead of loading them separately during instantiation.

Use the automatic loading function __autoload () provided by php to complete the process.

This function is automatically called when the script requires a class definition, but the class definition is not found on the page. At the same time, the required class name will be passed as a parameter to the _ autoload () function during the call: it will be executed every time. therefore, we get a chance (time) to load the corresponding class file through the execution of this function through the class name.

Note:

To ensure that automatic loading can be completed, we can find a way to get the class file name through the class name.

# P # php object-oriented programming-class constants # e #

Class constant:

Constants defined in the class

Definition:

Const constant name = constant value.

The defined constants do not contain class instantiation objects:

Call:

You cannot access this constant like a normal property:

Because class constants are saved in class space. Objects cannot be accessed. classes should be used for access.

If we need to use the class to access the content of the class space, we cannot use the-> operator. we should use the: operator. : Range parsing operator.

Note that class constants can only be accessed by classes. In other words, the constants of classes can be accessed as long as they can be found.

: Difference:
-> Operate on objects.

: Class operations

Generally, if the data is stored in the object space, the object access should be used. if the data is stored in the class space, the class access should be used. However, the method is special and always saved in the class. You can use objects or classes to access them.

If you need to call the constants of the current class within the class, you can use self to replace the current class.

What is the difference between Self $ this?

Self stands for class

$ This indicates the object.

# P # php object-oriented programming-static methods and static attributes # e #


Static members:

Static attributes and static methods

Static attributes and static methods are defined as static attributes or methods. attributes and methods belong to classes.

Static attributes:

Definition:

Defining static members only adds a static keyword to a non-static member.

Access: because it is saved in the class space, you should use the class name for access.

Objects do not need to be instantiated. Because a class is declared, there will be a class space.

Static method:

Definition:

Access:

Example:

Create an object counter. each time the counter value of an object is instantiated + 1, this counter stores the number of objects instantiated by the current class.

Find a container that can save the count: static property

Find the time to modify the count: constructor

Test results:

You can use an object to access non-static attributes, but not static attributes.

Test static and non-static methods:

The results show that static and non-static methods can be used regardless of the object or class.

What is the difference between a class call method and an object call method?

The difference is only on $ this.

$ This indicates the current object in the class method.

If you use an object to call a method, the current object is automatically (implicitly) passed to the method and $ this is used to reference the current object.

If you use a class to call a method, you cannot assign a value to $ this in the method.

What methods can be called by class?

Whether $ this is used in the method.

What is the difference between static and non-static methods?

When defining a static method, it indicates that this method should be accessed by class. Therefore, even if we use an object to call this method, we cannot pass the current object to this method.

Instead of static methods, you can pass the current object as a parameter to the method when calling an object. you can use $ this in the method to reference the object currently calling this method.

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.