Correction status:qualified
Teacher's comments:
<meta charset="utf-8"> <?php // 类的申明用关键字class class AllStart { //private私有属性 private $age; // public公共属性,一般用来自定义外部访问的接口的,如果自定义属性,就没有什么意义了,因为都可以访问 public $name; // protected 受保护的 protected $salary; public function __construct($age,$name,$salary) { $this->name; $this->age; $this->salary; } } //类的实例化就是对象哇,用关键字new; $AllStart=new AllStart(23,'swisun',2000); //对象是用变量来保存的 // var_dump($AllStart); $AllStart->$name; ?>
点击 "运行实例" 按钮查看在线实例
<meta charset="utf-8"> <?php /*1类常量与类属性的重载 2类的继承与方法的重写 3类中静态成员的申明与访问*/ // 类常量可以用来创建对象之间的共享数据,用关键字cons创建,因为是属于类,所以用类名加上范围解析符加上类常量名 /*在类的外部调用没有定义的属性或无权限访问的属性,会自动加载此类方法调用,属性重载的4个方法__get(),__set() __isset(),__unset();*/ class language { const word='hello world'; //命名一个类常量 private $name; private $userNumber; private $country; // 构造方法 public function __construct($name,$userNumber,$country) //__comstruct()是初始化类时候自动调用的 { echo $this->name=$name,'<br>'; echo $this->userNumber=$userNumber,'<br>'; echo $this->country=$country,'<hr>'; } public function __get($userNumber) //获取属性的重载 { if ($userNumber == 'grade') { return $userNumber . '不允许查看'; } return $this->$userNumber; } //更新属性的重载 public function __set($name, $value) { if ($name == 'grade') { echo $name.'不允许修改','<br>'; } $this->$name = $value; } //属性检测的重载 public function __isset($name) { if ($name == 'grade') { return false; } return isset($this->$name); } //销毁属性的重载 public function __unset($name) { if ($name == 'grade' || $name == 'name') { return false; } unset($this->$name); } } $language1=new language('php',6300,'english'); //echo language::word,'<br>'; //输出类常量,用类名加范围解析符::加其对应的名称 echo $language1->name,'<hr>'; echo $language1->userNumber,'<hr>'; //设置 $language1->country = 'YC'; echo '使用的国家: ', $language1->country,'<br>'; $language1->grade = 65; //销毁 unset($language1->country); // echo $language1->country,'<br>'; unset($language1->name); // 重载方法中__unset()不允许销毁grade和name属性 // echo $language1->name,'<br>'; // 所以这里仍可以访问到name属性 $language2=new language('python',4522,'english'); ?>
点击 "运行实例" 按钮查看在线实例
<meta charset="utf-8"> <?php // 类的继承与方法的重写 //继承是指类与类之间的继承,使用关键字extends // 引用父类成员用关键字parent:: class father { //创建3个父类属性 private $name; protected $age; public $sunNumber; const JACK='我爱学习php'; //创建父类构造方法 public function __construct($name,$age,$sunNumber) { echo $this->name=$name,'<br>'; echo $this->age=$age,'<br>'; echo $this->sunNumber=$sunNumber,'<hr>'; } } class father1 extends father { protected $country; public function __construct($age,$sunNumber,$country='chinese') { echo $this->age=$age,'<br>'; echo $this->sunNumber=$sunNumber,'<br>'; // parent::__construct($age,$sunNumber); echo $this->country=$country,'<hr>'; } } $father=new father('asdsa',23,46); $father=new father1('asdsa',23); ?>
点击 "运行实例" 按钮查看在线实例
<? php class Demo5 { public static $pdo = null; protected static $db = [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'php', 'user' => 'root', 'pass' => 'root', ]; public static function connect() { $dsn = self::$db['type'].':host='.self::$db['host'].';dbname='.self::$db['dbname']; self::$pdo = new PDO($dsn,self::$db['user'],self::$db['pass']); } public static function select($table,$fields='*', $num=5) { $stmt = self::$pdo->prepare("SELECT {$fields} FROM {$table} LIMIT {$num}"); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } } Demo5::connect(); $result = Demo5::select('staff','name,age,salary',8); echo '<pre>',var_export($result); ?>
点击 "运行实例" 按钮查看在线实例