• Classes

    A class in programming is like a group of people. For example, if you have a group of friends, each person in the group is called a "friend". In programming, we call each person in the group a "class". Just like your friends might all have different names and personalities, classes can also have different names and qualities. So when you write code, you can create different classes with different names and abilities to do things.

      • Object Classes

      • <?php
        class Person {
        	public $name, $age;
        
        	function __construct($name, $age) {
        		$this->name = $name;
        		$this->age = $age;
        	}
        
        	public function greet() {
        		print('Hello my name is ' . $this->name);
        	}
        }
        
        $human = new Person('John', 36);
        
        print($human->name); // John
        print($human->age); // 36
        $human->greet(); // Hello my name is John
      • Trait Classes

      • <?php
        trait HasFlippers
        {
        	public function swim()
        	{
        		return 'can swim';
        	}
        }
        
        trait HasFeathers
        {
        	public function fly()
        	{
        		return 'can fly';
        	}
        }
        
        class Duck
        {
        	use HasFlippers, HasFeathers;
        }
        
        class Seal
        {
        	use HasFlippers;
        }
        
        $a = new Duck();
        print($a->swim()); // can swim 
        print($a->fly()); // can fly 
        
        $b = new Seal();
        print($b->swim()); // can swim
      • Interface Classes

      • <?php
        interface Shape {
        	public function area();
        }
        
        class Rectangle implements Shape {
        	public $width, $height;
        
        	function __construct($width, $height){
        		$this->width = $width;
        		$this->height = $height;
        	}
        
        	public function area()
        	{
        		return $this->width * $this->height;
        	}
        }
        
        class Circle implements Shape {
        	public $radius;
        
        	function __construct($radius){
        		$this->radius = $radius;
        	}
        
        	public function area()
        	{
        		return pi() * $this->radius * $this->radius;
        	}
        }
        
        $a = new Rectangle(3, 5);
        $b = new Circle(4);
        
        printf('area of a is %f', $a->area()); // area of a is 15.000000
        printf('area of b is %f', $b->area()); // area of b is 50.265482
      • Struct Classes

      • <?php
        class Person
        {
        	public string $name;
        	public int $age;
        
        	function __construct(string $name, int $age)
        	{
        		$this->name = $name;
        		$this->age = $age;
        	}
        }
        
        $human = new Person('John', 36);
        
        print($human->name); // John
        print($human->age); // 36
      • Enum Classes

      • <?php
        enum Days: int
        {
        	case Sunday = 0;
        	case Monday = 1;
        	case Tuesday = 2;
        	case Wednesday = 3;
        	case Thursday = 4;
        	case Friday = 5;
        	case Saturday = 6;
        }
        
        $day = 4;
        
        if ($day == Days::Saturday->value || $day == Days::Sunday->value) {
        	print('a weekend');
        } else {
        	print('a weekday'); // a weekday
        }