实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/3 0003 * Time: 上午 11:16 */ class Person { private $name = ''; private $age = 0; private $sex = 0; private $weight = 0; private $salary = 0.0; private $level = 0; private $info = ''; //以下两种初始化用一种就行 public function __construct()//这个优先级高 {//__construct初始化 如果定义了这个放到 对象初始化前 不会报错 放后边就报错了---放这里就不报错 echo "2"; } public function Person() {//对象名初始化 echo "1"; } // public function __construct() // {//__construct初始化 这个貌似优先级高 如果定义了这个放到 对象初始化前 不会报错 放后边就报错了---放这里就报错 提示已经存在__construct // echo "2"; // } public function __get($name) { // TODO: Implement __get() method. return $this->$name; } public function __set($name, $value) { // TODO: Implement __set() method. $this->$name = $value; } public function setName($name){ $this->name = $name; } public function getName(){ return $this->name; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/3 0003 * Time: 上午 11:16 */ header("content-type:text/html;charset=utf-8"); require_once "class/Person.php"; $person = new Person(); echo "<pre>"; print_r($person); echo "</pre>"; echo $person->name='1'; echo "<br>"; echo $person->age=18; echo "<br>"; echo $person->salary=1800; echo "<br>"; echo $person->weight=105; echo "<br>"; echo $person->setName("楚留香"); echo "<br>"; echo $person->getName();
运行实例 »
点击 "运行实例" 按钮查看在线实例