In-depth PHP chapter III notes

Source: Internet
Author: User

Classes and objects:

A class is a template for an object, an instance of a class, and an instantiation of the data.

A class can produce n objects, but these objects are different individuals of the same type. Just like the different phones produced by the same mold, although they are the same mold, but are separate individuals, in the life cycle of the object, PHP will record each object.

The scope of the properties of the class:

For keywords, public,protected,private uses scopes to describe them more visually, because they can define whether the data can be accessed externally or internally, and specifies where the data is accessed.

About the data type of public:

Because PHP is a weakly typed language: You can dynamically add attributes to an object;

such as: Class Table {}

$table = new Table;

$table->field1 = ' filed1 ';//This is equivalent to adding a new attribute to the object, or overwriting the existing public data values, resulting in data variability, and insecurity. And in the use of the time and set the value of the time may be due to the word spelling errors caused by the increase in attributes, and the data is not correct.

So we have to strictly control the data types for public.

1: We should use constructors to initialize variables. Save effort and reduce our own word errors when defining variable values.

2: Use the function to return the data, reduce the error caused by the spelling of the word, because PHP will not prompt for the set of variables, but will prompt the function is not set, but also use the function is more beneficial to the implementation of the API, and error location, modification, and maintenance.

Parameters and types:

In addition to basic data types, such as characters, numbers, and Booleans, a class is also a data type, so that the object is the data type and the data type of the class.

As with variables, there is no need to define parameter types when defining parameters for PHP methods and functions, which makes it convenient and inconvenient. parameter is a large traversal for me by any type, but when a parameter of a particular type type is required, we have to verify the parameter type:

For basic data types, we do not use functions to verify:

Is_string,is_bool,is_numceric,is_null,is_resource,is_double and so on.

In object-oriented development, we remember to focus on development, ignoring context, which is based on important principles:

Three ways to resolve parameters:

Public function Test1 ($test) {

if (Is_bool ($test)) {}//mandatory judgment.

}

/*

* @param bool

*/

Public function Test1 ($test) {

if ($test) {}//gaze prompt.

}

Public function Test1 ($test) {

if (Preg_match ('/false|on|off/', $test)) {}//special character type.

}

In the PHP parameter type processing is very important, to provide good documentation.

Object parameter type:

PHP5 provides type hinting (type hint)

Public function test (class name Parameter name)//Support for object

Public function test (arrray parameter name)//support for array

About Inheritance:

First, we want to make sure that those attributes belong to the base class, and those attributes can be accessed by the quilt class.

The base class attribute must be defined in the base class if it is public, and all subclasses of the property that are likely to have attributes.

About overriding the parent method:

If you want to maintain the role of the parent method, you must use parent inside the function:: Method name (parameter value,.... )

Public,protected,private: Managing access to classes

Public: The properties and methods of the public type can be accessed everywhere;

Protected: Properties and methods that are accessible only within the class and inside the subclass

Private: Properties and methods that have access within the class

These visibility keywords: provide a clear interface to those parts of the service that we value to the public.

Recommendation: When accessing the properties of a class on the customer service side, it is best not to access it directly and to provide a way to access it. Such a method is called Getter,setter.

It is best to set private,protected for all properties.

The method can be set mostly to public unless you are sure that this method serves only within the class.

In-depth PHP chapter III notes

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.