PHP Class Properties and Methods

access modifiers: set the visibility of methods and member fields. php5 has three access modifiers. public, private, protected.

the public members can be accessed everywhere, both within the class and externally.

the private members is accessible only from within the class that defines it.

the protected members can only be accessed within the class itself or in descendant classes.


PHP class  properties

properties are used to add data to a class, its a class-specific variables.

<?php
           class    car{
           
                                public   $color  =  "red";
                    
                                private  $price  =  "$2000"; // class properties 
                    
                                protected $power =  "200hp";
                          }
               
                 $obj    =  new  car();
                   
        echo ( $obj->color ); // you can access  only public properties
                   
         /*  echo ( $obj->price ); 
        
this statement generate an error because private properties can't access outside the class*/

 ?>

php class methods

methods are class-specific function. method declare using the function keyword precedes a method name, followed by an optional list of argument variables in parentheses. the method body is enclosed by braces.

some quick points about the class methods:

  • methods must be declared in the body of class
  • methods can be declared public, protected, or private.
  • if you omit the visibility keyword in your method declaration, the method will be declared public implicitly.
<?php
           class    audi{
           
                                public   $colors  =  "red";
                    
                                private  $prices  =  "$2000"; // class properties 
                    
                                protected $powers =  "200hp";
                                
                                
      public  function  show(){  // access modifier must followed by function keyword
                                
                                
                                      echo  "audi colors = ". $this->colors;
                                        
                                      echo  "<br/> audi prices = ". $this->prices;
                                        
                                      echo  "<br/> audi powers = ". $this->powers;
                                        
                                    }       
                          }
               
                 $obj    =  new  audi();
                   
                 $obj->show(); // calling instance show() method
 ?>

php static class properties and methods

the static keyword is used to define static methods and properties.

  • static methods are callable without an instance of the object.
  • $this pseudo-variable is not available inside the method declared as static.

the static properties and methods accessed using the scope resolution operator( :: ).

syntax:

         classname :: method_name()
         classname :: static_property_name
<?php
           class    car{
           
                             static   public   $color  =  "red";   // static property
                    
                             static   private  $price  =  "$2000"; // static  property
                    
                                protected $power =  "200hp";      //  instance property
                                                    
 static  public  function   show(){
      
      
                               echo   "car  color = ". car::$color;
                               
                               echo   "<br/> car price = ". car::$price; echo "<br/>";
                               
           // you cannot use the instance  property within static method
           
           // $this variable cannot used within static method
           
                        }  
                        
 static  private   function  msg(){
 
                               echo "this is private static function";                    
                   } 
                   
                 }                        
               
               $obj    =  new  car();
                   
               car :: show(); // calling the static  show()  method
                 
 echo "access static public property outside the class = ". car::$color;
                 
// echo car:: $price ;  you cannot access private property outside the class
              
// car::msg() private static msg() method cannot accessed outside the class
 
  ?>

php class constants

class constants define with the const keyword. a constant variable cannot start with $ symbol.

syntax:

         const   price   =  "$40000" 

class constants variable access like static properties.

         classname::constant_variable_name

the value for constant variable must be a constant expression, not a variable, a property, a result of a mathematical operation, or a function call.

note : class constants cannot be made private or protected. they always publicly visible.

 <?php
            class   car{  // access modifires never be used with class constants 
            
                    const    price   =  "$40000";
                    
                    const    country =  "usa" ;
                    
                    public   $quantity  =  300 ;
                    
  public  function show(){
                    
                           $this->quantity = 500;
                                     
                           echo  "quantity = ".$this->quantity;
                           
                           echo  "<br/> constant price value = ".car::price;
                           
                           echo  "<br/> constant country value = ".car::country; 
                                     
                    
                            }                         
                    }
                    
                         $obj  = new  car();
                         
                         $obj->show();  
                         
                         echo  "<br/>price value outside the class = ".car::price;                  
  ?>                               

for good practice to use all-uppercase letters for class constant names( e.g. software_Id ).


PHP scope resolution operator( :: )

can be used to access static, constant, and overridden properties or methods of a class.

 <?php 
            class   car{
            
                            public   static   $value = "30000";
                    
                                       const    price = "$44000";                   
                         
            public  static function  show(){ echo  "<br/>this is a static method";}
            
                         } 
                           
                       echo  "value = ". car::$value;
                       
                       echo   "<br/>price = ".car::price;
                       
                       car::show();
                            
     ?>