Php class and object with constructor

Example with comments for better understanding:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
class Book{
//data members
private $name;
private $price;
//constructor
public function __construct($name,$price){
$this->name=$name;
$this->price=$price;
}
//show function for showing result
public function show(){
echo "$this->name <br>";
echo "$this->price <br>";
}
}
//object creation
$b = new Book("Song of ice and fire",67.89);
//function call
echo $b->show();
?>
</body>
</html>
view raw index.php hosted with ❤ by GitHub

Comments

Popular posts from this blog

C++ programming toll plaza simulation program