PHP - Custom Type Class constructors


Constructors are functions that are invoked when creating a new instance of the class.

Their name is always __construct, and that they do not have a return statement.

class Book { 
    public function __construct(int $isbn, string $title, string $author,  int $available) { 
        $this->isbn = $isbn; 
        $this->title = $title; 
        $this->author = $author; 
        $this->available = $available; 

The constructor takes four arguments, and then assigns the value of one of the arguments to each of the properties of the instance.

To instantiate the Book class, we use the following:

$book = new Book("2018", "C", 0003, 12); 

Related Topics