PHP static vs. Java static

Source: Internet
Author: User
In Java, static variables/static methods of classes are called class variables/class methods, they exist in the memory stack, only one copy, you can pass the class and ObjectsDirect access

public class Mystatic {public    static String className = "mystatic";    public static void GetClassName () {        //Of course you write directly classname can also be accessed, but only if there is no local variable name in this function overrides the static variable name        System.out.println ( mystatic.classname);    }        public static void Main (string[] args) {        //class accesses static variable        System.out.println (mystatic.classname);        The object accesses the 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 variable        System.out.println (mystatic.classname);        The object accesses the static variable        System.out.println ((New Mystatic ())). ClassName);        Class Access static method        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

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