Object-oriented keywords in PHP, PHP object-oriented keyword _php tutorial

Source: Internet
Author: User

Object-oriented keywords in PHP, PHP object-oriented keywords


Common keywords in PHP object-oriented are final, static, const

(1) Final:
1,final cannot modify member properties
2,final can only modify classes and methods

Role:
A class that uses final adornments cannot inherit from a quilt class
The final decoration method does not cover the quilt class.
Used to restrict the class from being inherited, the method cannot be overwritten, and the final
(2) Static:
1, use static to decorate member properties and member methods, not to decorate classes
2, a static decorated member property that can be shared by all objects of the same class
3, static data is present in the in-memory data segment (initialize static segment)
4, static data is allocated in memory every time the class is loaded, and is fetched directly from the data segment when the class is used later
5, as long as the class is used in the program (with this class name appearing), the class is loaded

Note: Static members are accessed using the class name, without creating an object, without having to access the object
Class Name:: Static member
If you use static members in your class, you can use self to represent this class (functionally equivalent to $this)
Self:: static member
6, static methods, non-static members cannot be accessed, non-static methods can access static members.
This is because non-static members must be accessed with objects, access to internal members using $this, static methods do not use objects to invoke, there is no object, $this can not represent what objects, non-static members must also use the object. If you determine that a method does not use a non-static member, you can declare the method as a static method (you cannot create an object and access it directly with the class name)
(3) Const:
1, it can only modify member properties
2, declaring a constant property in a class uses the const
3, the naming method and define are the same effect
4, the access mode is the same as the static statically member property: Class Name:: Constant self:: constant
5, constants must be given at the time of declaration to the initial value
6, constants cannot be re-assigned after declaration


What is the final keyword used in PHP object-oriented?

This is generally done to prevent a method of the parent class from being overridden.

The difference between public and Var in php object-oriented

Public and Var are almost the same because VAR defines variables that are public by default if they are not added protected or private

PHP4 is generally used in Var
Public is usually used in PHP5.

Now the basic is to use public instead of Var
var is defined as a variable, while public is the definition of the property (attribute) and method (methods) visibility

http://www.bkjia.com/PHPjc/908126.html www.bkjia.com true http://www.bkjia.com/PHPjc/908126.html techarticle PHP Object-oriented keywords, PHP object-oriented keyword PHP object- oriented common keywords are final, static, const (1) Final:1,final can not modify the member properties 2,final can only repair ...

  • 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.