Basic definition and concept of a class (extended 1)

Source: Internet
Author: User
Basic definition and concepts Of classes (extended 1) directory Table Of Contents

Constructor

Any given parameter is passed to the constructor.

Class ShopProduct {function _ construct ($ title, $ firstName, $ mainName) {// special syntax _ construct }}$ product1 = new ShopProduct ('this is title', 'Peter ', 'Peter Yuan') of the constructor '); // Here is the input parameter, which is passed into the class or the constructor directly. the parameters are arranged in order.

The constructor is automatically executed when the class is created.

Reminders

Because php is a weak language, you do not need to specify the data type during variable declaration. This makes it convenient, but also brings a bit of security issues, so you need to add some measures. if necessary, add some type judgments, such as is_bool () to determine whether a Boolean value exists, and return true false.

      function outputAddresses($resolve){        if(! is_boll ($resolve)){          XXXXXXXXXXXXXX        }      }

2. Add comments such:

/*** Determine if the given abstract type has been bound. ** @ param string $ abstract // announce the input parameter type * @ return bool // announce the returned value */public function bound ($ abstract) {$ abstract = $ this-> normalize ($ abstract); return isset ($ this-> bindings [$ abstract]) | isset ($ this-> instances [$ abstract]) | $ this-> isAlias ($ abstract );}

In object-oriented development, "focusing on specific tasks and ignoring external context" is an important design principle. (In my personal understanding, this is a special task. for special cases, methods and classes only need to focus on their own tasks, so that errors can be avoided .)

You need to carefully weigh between the detection type, conversion type, and documentation that depends on Niang Muyi. (However, documents should be provided no matter which one should be used)

We cannot rely on compilers to prevent type-related bugs. we must consider the consequences of passing invalid data type parameters to methods. we cannot fully trust client programmers, always consider how to process useless information they introduce in methods.

Type hint)
  1. Type prompt can detect the type of parameters used
  2. The method definition is clearer and it is easier to know the input parameter type.
Class ShopProduct {} class ShopProductWriter {public function write ($ shopProduct) {}}$ product1 = new ShopProduct (); $ writer = new ShopProductWriter (); $ writer-> write ($ product1); when there is a type prompt, public function write (ShopProduct $ shopProduct) {// you can pass in only when the ShopProduct object is specified, this is the type prompt }...... $ writer-> write ($ product1); // The code can be automatically detected when it is passed in directly. if it is not a ShopProduct object, an error will be reported.

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.