码迷,mamicode.com
首页 > Web开发 > 详细

PHP对象

时间:2019-12-19 19:19:09      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:echo   print   构造   private   sel   自动生成   UNC   sch   end   

<?php

class Person{
public $name;
public $age;
public static $ss = "第一个";

function __construct($name,$age){
// TODO:实现构造函数()方法
$this->name=$name;
$this->age=$age;
}

function say(){ // 公有
echo "名字:" . $this->name . "\n";
echo "年龄:" . $this->age . "\n";
echo "性别:" . $this->xingB() . "\n";
echo "学校:" . $this->school() . "\n";
echo self::$ss . "\n";
}

private function xingB(){ // 私有
return "男生";
}

protected function school(){ // 受保护
return "xu";
}

protected function play(){
echo "12345 ";
}

function __destruct()
{
// TODO: Implement __destruct() method.
// TODO:实现析构函数()方法
echo "父类 再见" . $this->name . "\n";
}
}


class sonRen extends Person{

function saySon(){
echo "名字是" . $this->name . "\n";
echo "年龄是" . $this->age . "\n";
echo "性别是" . $this->xingB() . "\n";
echo "学校是" . $this->school() . "\n";
}

public function xingB(){ // 子类私有方法在这里无法重写
$nan = "女生";
return $nan;
}

public function school(){ // 子类可以更改父类的受保护方法
return "pt";
}

public function play()
{
parent::play(); // TODO: Change the autogenerated stub
// TODO:更改自动生成的存根
echo "6789\n";
}

function __destruct()
{
// TODO: Implement __destruct() method.
// TODO:实现析构函数()方法
echo "子类 拜拜" . $this->name . "\n";
}
}


$r = new Person("san",18); // 父类访问父类方法
print_r($r->say() . "\n");

$s = new sonRen("wu",20); // 子类访问子类方法
print_r($s->saySon() . "\n");

print_r($s->play() . "\n"); // 子类访问父类

print_r($s->say(). "\n"); // 子类访问父类方法

PHP对象

标签:echo   print   构造   private   sel   自动生成   UNC   sch   end   

原文地址:https://www.cnblogs.com/see-you-cu/p/12069478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!