computer.class.php
<?php
class Computer {
public $_name = 'dell';
public function _run() {
echo '运行';
}
}
?>
person.class.php
<?php
class Person {
}
?>
demo1.php
<meta charset="UTF-8">
<?php
//引入类文件即可
//require 'computer.class.php';
//1.如果要包含多个类文件,是不是要一一引入。
//2.引入多个类文件,如果有用不到的,就会产生浪费
//3.引入了类文件,可能会遗漏,比如说,创建了一个对象,而这个对象的类文件没有引入。就会产生错误
//$_className = 类名
//只要实例化了,那么 $_className = 'Computer';
function __autoload($_className) {
require strtolower($_className).'.class.php';
}
$computer = new Computer();
$person = new Person();
echo $computer->_name;
$computer->_run();
?>
demo2.php
<meta charset="UTF-8">
<?php
class Computer {
public function _run() {
echo '运行';
}
//采用__call()方法屏蔽调用
//__call()里面有两个参数
public function __call($_methodName,$_argList) {
echo $_methodName.'()方法不存在';
print_r($_argList);
}
}
$computer = new Computer();
$computer->_go();
?>
demo3.php
<meta charset="UTF-8">
<?php
class Computer {
public function _run() {
echo '运行';
}
public function __toString() {
return '我是对象的字符串!';
}
}
// $computer = new Computer();
// echo $computer;
echo new Computer();
?>
demo4.php
<meta charset="UTF-8">
<?php
class Computer {
public $_name = 'dell';
public function _run() {
echo '运行';
}
}
//引用传递
$computer1 = new Computer();
$computer2 = $computer1;
$computer2->_name = 'ibm';
echo $computer1->_name;
?>
demo5.php
<meta charset="UTF-8">
<?php
class Computer {
public $_name = 'dell';
public function _run() {
echo '运行';
}
}
//独立实例
$computer1 = new Computer();
$computer2 = new Computer();
$computer1->_name = '联想';
echo $computer1->_name;
?>
demo6.php
<meta charset="UTF-8">
<?php
class Computer {
public $_name = 'dell';
public function _run() {
echo '运行';
}
}
//对象克隆
$computer1 = new Computer();
$computer1->_name='联想';
$computer2 = clone $computer1;
//echo $computer2->_name;
$computer2->_name = 'ibm';
echo $computer2->_name;
echo $computer1->_name;
?>
demo7.php
<meta charset="UTF-8">
<?php
class Computer {
public $_name = 'dell';
public function _run() {
echo '运行';
}
public function __clone() {
echo '克隆了';
$this->_name = '联想';
}
}
$computer1 = new Computer();
$computer2 = clone $computer1;
echo $computer1->_name;
echo $computer2->_name;
?>
demo8.php
<meta charset="UTF-8">
<?php
class Computer {
public function _run() {}
private function _go() {}
public $_name = 'dell';
private $_model = 'i7';
}
class NoteComputer extends Computer {
}
interface Person {
}
$computer = new Computer();
$notecomputer = new NoteComputer();
//1.检查类是否存在
echo class_exists('Computer');
//2.获取对象的类名
echo get_class($computer);
//3.获取类中公共的方法
print_r(get_class_methods($computer));
//4.获取类中的字段
print_r(get_class_vars('Computer'));
//5.获取子类的父类
echo get_parent_class($notecomputer);
//6.判断接口是否存在
echo interface_exists('Person');
//7.判断对象是否是这个类,$notecomputer的类的父类是Computer
echo is_a($notecomputer,'Computer');
//8.判断对象是否是类的子类。
echo is_subclass_of($notecomputer,'Computer');
//9.判断对象是否有这个方法
echo method_exists($computer,'_run');
?>
demo9.php
<?php
//反射,了解一下,入门
//查看自定义类
class Computer {
public function _run() {}
private function _go() {}
public $_name = 'dell';
private $_model = 'i7';
public static $_cpu;
public static function tool() {
}
const PI = 3.14;
}
//使用反射列出他们的信息
Reflection::export(new ReflectionClass('Computer'));
?>
demo10.php
<?php
class Computer {
}
//系统内置的类
//Reflection::export(new ReflectionClass('DateTime'));
$rc = new ReflectionClass('DateTime');
echo $rc->getName();
echo $rc->getFileName();
print_r($rc->getConstants());
?>
标签:第三章,name,computer1,computer2,echo,面向对象,computer,工具,php From: https://blog.51cto.com/u_16171388/6558646