PHP new class instantiation

巴扎黑
Release: 2016-11-22 10:13:36
Original
2127 people have browsed it

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;
}
}
?>
Copy after login

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();
}
?>
Copy after login

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

Zhang San
Li Si
Wang Wu


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!