sudhakarinfotech
(current)
Run Code
Live Code Editor- Need Of PHP Access Modifiers
Source Code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Access Modifier Requirement</title> </head> <body> <h1>Access Modifier Requirement</h1> <?php class Car { //the private access modifier denies access to the property from outside the class’s scope private $model; //the public access modifier allows access to the method from outside the class public function setModel($model) { //validate that only certain car models are assigned to the $carModel property $allowedModels = array("Mercedes benz","BMW"); if(in_array($model,$allowedModels)) { $this -> model = $model; } else { $this -> model = "not in our list of models."; } } public function getModel() { return "The car model is " . $this -> model; } } $mercedes = new Car(); //Sets the car’s model $mercedes -> setModel("Mercedes benz"); //Gets the car’s model echo $mercedes -> getModel(); ?> </body> </html>
Source Code Output :
Running