Search code snippets, questions, articles...

Create constructor in PHP OOP

You can create a constructor of a class using __construct() function
    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();

Search Index Data (The code snippet can also be found with below search text)

__construct() function PHP
Was this helpful?