freeCodeCamp/guide/english/php/class/index.md

30 lines
1.1 KiB
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: PHP - Class
---
### Simple Class for Beginner!
```php
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
2018-10-12 19:37:13 +00:00
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
}
2018-10-12 19:37:13 +00:00
private function getName() { // getName is getter function that returns the value of instance variable $name
2018-10-12 19:37:13 +00:00
return $this->name;
}
public function say_my_name() {
$name = $this->getName();
return $name;
}
}
$breaking_bad = 'Heisenberg';
$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
2018-10-12 19:37:13 +00:00
echo "My Name is " . $lab->say_my_name(). "!";
```