Added description of class sections (#22306)
Added description for class definition, instance variables, setter and getter functions, creation of instance and calling a function on it
This commit is contained in:
@@ -5,14 +5,14 @@ title: PHP - Class
|
||||
### Simple Class for Beginner!
|
||||
|
||||
```php
|
||||
class Lab {
|
||||
private $name = '';
|
||||
class Lab { // class keyword is mandatory identifier for class creation, after class keyword goes the name of the class(e.g. Lab)
|
||||
private $name = ''; // $name is instance variable, which means that every instantiated object has it's own copy of variable $name
|
||||
|
||||
public function setName($name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
public function setName($name) { // function setName is setter function that sets the value of instance variable $name
|
||||
$this->name = $name; // because $name is the name of both instance variable and function parameter, we use $this keyword
|
||||
}
|
||||
|
||||
private function getName() {
|
||||
private function getName() { // getName is getter function that returns the value of instance variable $name
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ class Lab {
|
||||
|
||||
}
|
||||
$breaking_bad = 'Heisenberg';
|
||||
$lab = new Lab();
|
||||
$lab->setName($breaking_bad);
|
||||
$lab = new Lab(); // keyword new creates instance of Lab class, variable $lab points to that instance
|
||||
$lab->setName($breaking_bad); // $lab variable that points to Lab instance calls setter function setName with $breaking_bad as parameter
|
||||
echo "My Name is " . $lab->say_my_name(). "!";
|
||||
```
|
||||
|
Reference in New Issue
Block a user