Home  >  Article  >  Backend Development  >  PHP new class instantiation

PHP new class instantiation

巴扎黑
巴扎黑Original
2016-11-22 10:13:362158browse

Define the class BBB and save it as BBB.php. The content is as follows:

<?php 
class BBB{
private $name;
function __construct($name){
$this->name = $name;
}
function hello() {
echo $this->name;
}
}
?>

Write a php file for testing and save it as AAA.php. The content is as follows:

<?php 
require_once &#39;BBB.php&#39;;
if(class_exists(&#39;BBB&#39;)){
$bbb = new BBB(&#39;张三&#39;);
$bbb->hello();
echo "<br>";
$class = BBB;
$bbb = new $class(&#39;李四&#39;);
$bbb->hello();
echo "<br>";
$class = &#39;BBB&#39;;
$bbb = new $class(&#39;王五&#39;);
$bbb->hello();
}
?>

Access AAA.php through the browser and the output result is as follows:

Zhang San0c6dc11e160d3b678d68754cc175188aLi Si0c6dc11e160d3b678d68754cc175188aWang Wu


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php command line modeNext article:php command line mode