[Object-OrientedBootcamp] 7-Statics-and-Constants

Source: Internet
Author: User
[Object-OrientedBootcamp] 7-Statics-and-Constants you may see some articles on the Internet, which mention static is harmful. What do they mean? Is this true? Okay. let's talk about this in this section.

Learn through examples

Before the transformation, the dynamic call is as follows:

 Add (1, 2, 3); // result int (6)

After the original dynamic transformation, the static call is as follows:

     

After being transformed to a static method, you can directly call it using the class name.

Another example

Static variable usage:

  HaveBirthday (); $ joe-> haveBirthday (); $ jane = new Person (); $ jane-> haveBirthday (); echoPerson: $ age; // not 2, the result is 4. The $ age variable is static and shared.

Const identifier:

      

Use in Laravel

In Illuminate \ Support, the Str class uses a large number of static variables and static methods, for example:

EchoStr: lower ("FOOBAR"); // Result: foobarechoStr: camel ();

Reference link:

* PHP5: const vs static

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.