PHP var keyword

PHP

Note.The var keyword has been deprecated from version 5.0.0 to version 5.1.2. Since PHP 5.1.3 it has been added again.Syntax:
class className {var $variable = "GeeksforGeeks"; // Other statements} 
The following programs illustrate the varkeyword in PHP:Program 1:this program illustrates the keyword var.  
// Class declaration class Geeks { // Use the var keyword // declare a public variable var $var1 = ' Public' ; // Decalring protected variable protected $var2 = ' Protected' ; // Decalrng private variable private $var3 = ' Private' ; }  
// Create an object $obj = new Geeks();  
// Call the declared variable var echo $obj -> var1. "" ;;  
?>
Output:
Public 
Program 2:This program illustrates the varand public .  
// Class declaration class Geeks { // Use the var keyword // declare a public variable var $var1 = 'Var Public' ; // Use a public keyword // declare a public variable public $var2 = 'Public' ;  
}  
// Create an object $obj = new Geeks();  
// Call the declared variable var echo $obj -> var1. "" ;  
// Call the publicly declared variable echo $obj -> var2. "" ;;  
?>
Output:
Var Public Public 
Program 3:This program shows an error when calling private variables.  
// Class declaration class Geeks { // Use var keyword // declare a public variable var $var1 = 'Var Public' ; // Use a private keyword // declare a private variable private $var2 = 'Private' ;  
}  
// Create an object $obj = new Geeks();  
// Call the declared variable var echo $obj -> var1. "" ;;  
// Call a private declared variable
// throws an error echo $obj -> var2. "" ;;  
?>
Output:
Var Public 
Error :
PHP Fatal error: Uncaught Error: Cannot access private property Geeks::$var2 in / home / 46488c166fd1197d687867f62e03b8b8 .php: 24 Stack trace: # 0 {main} thrown in /home/46488c166fd1197d687867f62e03b8b8.php on line 24 




Get Solution for free from DataCamp guru