Search code snippets, questions, articles... Chrome Extension

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", "john@domail.com");
    $student_info = $student->get_student_info();
?>
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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

__construct() function PHP
Was this helpful?
0 Comments
Join Devsheet Ask a Question