class modifiers and access modifiers for PHP

Source: Internet
Author: User
Tags modifiers
This article mainly introduces the class modifier of PHP and access modifiers, has a certain reference value, now share to everyone, the need for friends can refer to

class modifier

    • 1.abstract decorated class is abstract class, if a class contains abstract methods then this class is abstract class (of course, there is no abstract method in a class, we can also define this class as abstract class), then what is abstract method, abstract method is no method body method (no curly braces and the contents of the inside), The abstract method is preceded by an abstraction modifier. The abstract class itself cannot be instantiated, and only one class inherits it, overwriting all of its abstract methods so that we can instantiate its subclasses

Abstract class my{    abstract function Say ();} Abstract class my{     function Say () {        echo ' hello ';    }}
    • 2.interface decorated classes are interfaces, interfaces are somewhat similar to abstract classes, but the difference is that all methods of an interface are abstract methods, and the interface's abstract method is not preceded by an abstract adornment, and the member property must be a constant. Interface references differ from class-inheriting keyword extends, where inheritance can only be singular, and interfaces may use keywords to implements multiple references and separate them with commas.

Interface Demo {Const NAME = "Constant object property"; function fun1 (); function fun2 ();//abstract method. } class MyPc extends Root implements demo, Demo2, Demo3 {...}
    • A 3.final decorated class is an end class, and it cannot be inherited.

Access modifiers

    • 1.public public methods and properties that can be used in all classes

    • 2.static static methods and variables

If the access Control permission allows, you do not have to create the class object and directly use the class name plus two colons "::" Call, static properties, only one copy in memory, for all instances of the common, static methods cannot call non-static properties. You cannot use Self:: Call a non-static property.

function test () {static $a =1; $a *=2;echo $a. " \ n ";} Test () test () test ()//print result//2//4//8

The initialization of a static variable is assigned only once, and the subsequent test method is no longer initialized.

    • 3.private modified methods and variables are private and can only be called in this class (instantiating this class in other classes, methods and variables that cannot invoke private adornments)

    • 4.proteced protected methods and variables that can be used only in subclasses of this class and in this class

    • The 5.final modified method cannot be overridden.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.