php

Create constructor in PHP OOP

You can create a constructor of a class using __construct() function

<?php
    class Student {
        public $name;
        public $email;

        function __construct($name, $email) {
            $this->name = $name;
            $this->email = $email;
        }

        function get_student_info() {
            return "name: " . $this->name . ', email: '. $this->email;
        }
    }

    $student = new Student("John", "[email protected]");
    $student_info = $student->get_student_info();
?>
Was this helpful?