PHP static vs. Java static

Source: Internet
Author: User

Static variables/static methods of classes in Java are also called class variables/class methods, they exist in the memory stack, only one copy, can be directly accessed through classes and objects

Public class mystatic {    public static string classname  =  "Mystatic";     public static void getclassname ()  {         //Of course you can also access the direct write classname, but only if there is no local variable name in this function override static variable name          system.out.println (Mystatic.classname);    }         public static void main (String[] args)  {         //class Access static variables          System.out.println (mystatic.classname);         //object access static variable          system.out.println ((New mystatic ()). ClassName);         //class Access static method         mystatic.getclassname () ;          //object Access static method          (New mystatic ()). GetClassName ();         //=========         (New  mystatic ()) .classname =  "New class name";         //=========        //class Access static variables          system.out.println (mystatic.classname);         //object access static variable         system.out.println ((New mystatic ()). ClassName);         //class Access static methods          Mystatic.getclassname ();         //object access static method           (New mystatic ()). GetClassName ();     }}

Note that Java static variables do not prohibit access to the object, but PHP is different, the static method of PHP can be called by the object, but the static variables of the class can only be accessed by the method of the class, the object is not directly accessible

Class Mystatic {public static $className = __class__;    public static function GetClassName () {echo self:: $className; }}echo mystatic:: $className; Mystatic::getclassname ();//but (new Mystatic ())->classname;//error No permissions//but below is Possible (new Mystatic ()) GetClassName ();//static only one copy, everyone together hard connection, synchronous modification mystatic:: $className = "New Class Name";(new Mystatic ())->getclassname ();

So the PHP object must rely on the interface to access the static variables of the class, and write a method of accessing the static variable in the class, rather than using it directly like Java. syntax classes and objects are accessible

PHP static vs. Java 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.