PHP 5: const vs static

前端 未结 7 796
长情又很酷
长情又很酷 2020-11-29 16:38

In PHP 5, what is the difference between using const and static?

When is each appropriate? And what role does public, pr

7条回答
  •  有刺的猬
    2020-11-29 17:29

    Here's the things i learned so far about static members , constant variables and access modifiers(private,public and protected). Constant

    Definition

    Like the name says values of a constant variable can't be changed.Constants differ from normal variables in that you don't use the $ symbol to declare or use them.

    The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.

    Note : The variable's value can not be a keyword (e.g. self, parent and static).

    Declaring a constant in php

    
    

    Constant's scope is global and can be accessed using a self keyword

    showConstant();
    
    echo $class::CONSTANT."\n"; // As of PHP 5.3.0
    
    ?>
    

    Static

    Definition

    Static keyword can be used for declaring a class, member function or a variable.Static members in a class is global can be accessed using a self keyword as well.Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can). If no visibility declaration ( public, private, protected ) is used, then the property or method will be treated as if it was declared as public.Because static methods are callable without an instance of the object created.

    Note : the pseudo-variable $this is not available inside the method declared as static.Static properties cannot be accessed through the object using the arrow operator ->

    As of PHP 5.3.0, it's possible to reference the class using a variable. The >variable's value cannot be a keyword (e.g. self, parent and static).

    Static property example

    
    

    Accessing static properties and functions example

     staticValue() . "\n";
        print $foo->my_static . "\n";      // Undefined "Property" my_static 
    
        print $foo::$my_static . "\n";
        $classname = 'Foo';
        print $classname::$my_static . "\n"; // As of PHP 5.3.0
    
        print Bar::$my_static . "\n";
        $bar = new Bar();
        print $bar->fooStatic() . "\n";
    
     ?>
    

    Public, private , protected (A.K.A access modifiers)

    Before reading the definition below , read this Article about Encapsulation .It will help you to understand the concept more deeply

    Link 1 wikipedia

    Tutorials point link about encapsulation

    Definition

    Using private , public , protected keywords you can control access to the members in a class. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

    Example

      
    

    Accessing the public, private and protected members example

    Public variable's can be accessed and modified from outside the class or inside the class. But You can access the private and protected variables and functions only from inside the class , You can't modify the value of protected or Public members outside the class.

      $pbVariable;  //public variable 
         echo $this->$protVariable;  //protected variable
         echo $this->privVariable; //private variable
        }
    
       private function PrivateFun(){
    
     //some statements
      }
      protected function ProtectedFun(){
    
     //some statements
      }
    
      }
    
    
     $inst = new Example();
     $inst->pbVariable = 'AnotherVariable'; //public variable modifed from outside
     echo $inst->pbVariable;   //print the value of the public variable
    
     $inst->protVariable = 'var'; //you can't do this with protected variable
     echo $inst->privVariable; // This statement won't work , because variable is limited to private
    
     $inst->publicFun(); // this will print the values inside the function, Because the function is declared as a public function
    
     $inst->PrivateFun();   //this one won't work (private)
     $inst->ProtectedFun();  //this one won't work as well (protected)
    
      ?>
    

    For more info read this php documentation about visibility Visibility Php Doc

    References : php.net

    I hope you understood the concept. Thanks for reading :) :) Have a good one

提交回复
热议问题