public $salary = 6720.12; public $info = array('a' => 1, 'b' => 2, 'c' => 3); // 方法 function say($sth) { echo $this->name, '说', $sth, '<br/>'; } function study($subject) { echo $this->name, '在学习', $subject, '<br/>'; } function sleep() { echo $this->name, '在睡觉', '<br/>'; } } // 创建对象 $p1 = new person(); $p1->name = '张三'; $p1->study('面向PHP对象'); // print_r($p1); // 创建对象 $p2 = new person(); $p2->name = '李四'; $p2->say('hello world'); // print_r($p2); // 创建对象 $p3 = new person(); $p3->name = '赵五'; $p3->sleep(); // print_r($p3);
echo "<hr/>"; //clone 关键词 $p1 = new person("张三", "男", "88"); $p2 = clone $p1; $p1->name = "王麻子"; echo "<br/>"; echo $p1->name; echo $p2->name; echo "<hr/>"; //魔术方法 __toString() ————让 'echo $p1'不报错 echo $p1; echo "<hr/>"; //魔术方法 __call() $p1->run(); echo '<br/>'; $p1->say('爬', '开'); class DB { private $sql = array("field" => "", "where" => "", "order" => "", "limit" => "", "group" => "", "having" => ""); function __call($DBName, $args) { $DBName = strtolower($DBName); if (array_key_exists($DBName, $this->sql)) { $this->sql[$DBName] = $args[0]; } else { echo '你调用的' . get_class($this) . '中的方法' . $DBName . '不存在!'; } return $this; } function select() {
{ var $name; var $sex; var $age; function setName($name) { $this->name = $name; } function getName() { return $this->name; } function say() { echo "my nane is:" . $this->name . "sex is:" . $this->sex . "age is:" . $this->age . "<br>"; } function run() { echo $this->name . "this person on stree"; } function __destruct() { echo "goodbye" . $this->name . "<br>"; } } $p1 = new person(); $p2 = new person("猪", "woman", 30); $p1->setName("猪"); // $p3 = new person ( "sunfolwer", "man", 25 ); $p1->say();
public function say() { parent::say(); // 调用父类中的方法 echo $this->name, "\tis\t", $this->gender, ",and is\t", $this->age, PHP_EOL; }
class person { public $name; public $gender; public function say() { echo $this->name . "\tis ", $this->gender, "\r\n"; } } class family { public $people; public $location; public function __construct($p, $loc) { $this->people = $p; $this->location = $loc; } } $student = new person(); $student->name = 'Tom'; $student->gender = 'male'; $student->say(); $tom = new family($student, 'peking'); echo serialize($student); $student_arr = array('name' => 'Tom', 'gender' => 'male'); echo "\n"; echo serialize($student_arr); print_r($tom); echo "\n"; echo serialize($tom);