Home > Article > Backend Development > PHP new class instantiation
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 'BBB.php'; if(class_exists('BBB')){ $bbb = new BBB('张三'); $bbb->hello(); echo "<br>"; $class = BBB; $bbb = new $class('李四'); $bbb->hello(); echo "<br>"; $class = 'BBB'; $bbb = new $class('王五'); $bbb->hello(); } ?>
Access AAA.php through the browser and the output result is as follows:
Zhang San0c6dc11e160d3b678d68754cc175188aLi Si0c6dc11e160d3b678d68754cc175188aWang Wu