Php class and object with constructor
Example with comments for better understanding:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Comments
Post a Comment
Feel free to leave a comment...