Classes and Objects
A class holds properties and methods. new creates an object and -> accesses members. $this is the current object.
Example
class Dog {
public $name;
function __construct($n) { $this->name = $n; }
function bark() { return "{$this->name} woofs"; }
}
$d = new Dog("Rex");
echo $d->bark();Result
Rex woofs
Exercise
Create an object.
$d = Dog("Rex");
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.