whitek 发表于 2017-4-5 12:50:08

php入门之 类的定义、构造函数

  php类的定义方法,以及构造函数的使用,变量赋值,变量取值。
  


<?php
class Animal{
private $name;
//构造函数
//用 _ _construct()做构造函数( 注意,是两个紧跟着的_)
function __construct($text){
$this->name = $text;
}
//赋值
function set_name($text){
$this->name = $text;
}
//取值
function get_name(){
return $this->name;
}
}
$lion = new Animal("tomcat");
//$lion->set_name("Leo");
echo "Animal Name is ", $lion->get_name(), ".", "test"."122";
echo "<br />";
$color = "red";
echo "Roses are $color";
echo "<br />";
//单引号和双引号的不同之处。单引号仅输出变量名,双引号输出变量值:
echo 'Roses are $color';
?>
<!--第二种输出方法-->
<p>Roses are <?=$color?></p>
php类的定义方法,以及构造函数的使用,
 
页: [1]
查看完整版本: php入门之 类的定义、构造函数