PHP开发圣经-第六章

<pre name="code" class="php"><?php
/**
 * Created by PhpStorm.
 * User: v_szylchen
 * Date: 2015/9/21
 * Time: 11:19
 * 第六章 面向对象的PHP
 */
?>


<?php
/**
 * public 表示全局,类内部外部子类都可以访问;
 * private表示私有的,只有本类内部可以使用;
 * protected表示受保护的,只有本类或子类或父类中可以访问;
 * 构造函数__construct 实例化一个类时执行
 * 析构函数__destruct 销毁一个类前执行
 *
 */
class test {
	public $name;
	public $age;
	public $sex;
	public function __construct($_name, $_age, $_sex) {
		$this->name = $_name;
		$this->age = $_age;
		$this->sex = $_sex;
	}
	public function __destruct() {
		echo "hey" .$this->name ."<br/>";
	}
}

$test1 = new test("Raye", "20", "male");
$test2 = new test("Alex", "22", "female");
$test3 = new test("Brant", "26", "male");
/**
 * __destruct调用顺序是test3 test2 test1;
 * 每次new关键字会实例化一个对象,并在堆内存里开辟一块自己的空间,并且在每个空间中都存有该类中声明的成员。但是对象的引用都是存放在栈内存中的,由于栈的后进先出的特点,最后创建的对象引用会被最先释放。
 */

$test4 = new test("Raye", "20", "male");
$test4 = null;
$test5 = new test("Alex", "22", "female");
$test6 = new test("Brant", "26", "male");
/**
 * __destruct调用顺序是test4 test6 test5
 * 创建后就被赋予了空值,所以这个对象最先失去引用,从而最先调用析构方法。
 */

?>


<?php
/**
 * 重载 重写父类方法
 * final 禁止继承和重载 可用在类或方法
 */

class parentClass {
	public $name = "caesar";
	public function parentFunction() {
		return "hello" .$this->name ."<br/>";
	}
}
class subClass extends parentClass {
	public $name = "linux";
	public function parentFunction() {
		return "bye" .$this->name ."<br/>";
	}
}
class subClass2 extends parentClass {
	public $name = "Bob";
	public function parentFunction() {

	}
/**
 * 重载后想调用父类方法 parent::function 注意 变量值是当前类的
 */
	public function subFunction() {
		return parent::parentFunction();
	}
}
$subClass1 = new subClass();
echo $subClass1->parentFunction();

$subClass2 = new subClass2();
echo $subClass2->subFunction();


final class finalTest {
	public function test() {

	}
}

/**
class finalTest2 extends finalTest {

 //Fatal error: Class finalTest2 may not inherit from final class

}
 */

class finalFTest {
	public final function finalF() {
		return TRUE;
	}
}

/**
class finalFTest2 extends finalFTest {
	public final function finalF() {
		return FALSE;
	}
}
 * Fatal error: Class finalFTest2 may not inherit from final class
 *
 */

?>


<?php
/**
 * 实现接口 用于实现多重继承
 * 某个子类继承某个父类并实现多个接口
**/
interface ISetName {
	function setName($name);
}
interface IGetName {
	function getName();
}

class all {

}
class custom extends all implements ISetName,IGetName {
	private $name;
	function setName($_name) {
		$this->name = $_name;
	}
	function getName() {
		return $this->name;
	}
}

$customA = new custom();
$customA->setName("Tidy");
echo $customA->getName() ."<br/>";

?>


<?php
/*
 * 抽象类
 * 个人理解 抽象类相对于普通类来说 首先抽象类不能实体化 如果抽象类中有抽象方法的定义 那么子类就必须实行抽象类的抽象方法,跟接口的思想一样,
 * 但是只能继承一个抽象类,可以继承多个接口,抽象类跟接口的区别是,抽象类中的抽象方法是子类共有的并且还可以定义一些普通方法
 * 使用情况的话 抽象类用在子类有共同的方法不同的实现,接口是某些特定子类才有的方法
 */

abstract class Shape {
	abstract protected function get_area();

}
class Rectangle extends Shape{
	private $width;
	private $height;
	function __construct($width=0, $height=0){
		$this->width = $width;
		$this->height = $height;
	}
	function get_area(){
		echo ($this->width+$this->height)*2 ."<br/>";
	}
}
$Shape_Rect = new Rectangle(20,30);
$Shape_Rect->get_area();

?>


<?php
/**
 * 常量 不需实例化就能访问
 * 静态方法 方法中不能使用this
 * 延迟静态绑定 通过使用静态作用域,可以强制PHP在最终的类中查找所有属性的值
 * 类型检查 (1)判断一个对象是否是某个类的实例,(2)判断一个对象是否实现了某个接口。
 */

//常量
class Math {
	const pi = 3.1415;
}
echo Math::pi ."<br/>";

//静态方法
class Math2 {
	static function squared($input) {
		return $input * $input;
	}
}
echo Math2::squared(20) ."<br/>";

//延迟静态绑定
class A {
	public static function who() {
		echo __CLASS__;
	}
	public static function test() {
		static::who();
	}
}
class B extends A {
	public static function who() {
		echo __CLASS__ ."<br/>";
	}
}
B::test();
//类型检查
$t = new B();
echo $t instanceof B ."<br/>";
echo $t instanceof A ."<br/>";

interface ExampleInterface
{
	public function interfaceMethod();
}

class ExampleClass implements ExampleInterface
{
	public function interfaceMethod()
	{
		return 'Hello World!';
	}
}

$exampleInstance = new ExampleClass();

if($exampleInstance instanceof ExampleInterface){
	echo 'Yes, it is' ."<br/>";
}else{
	echo 'No, it is not';
}
/*
* */
function check_hint(A $someClass) {

};
check_hint($t);

?>


<?php
/**
 * __clone  当对象被复制后,会对对象的所有属性执行一个浅复制(shallow copy)。所有的引用属性 仍然会是一个指向原来的变量的引用
 * 当复制完成时,如果定义了 __clone() 方法,则新创建的对象(复制生成的对象)中的 __clone() 方法会被调用,可用于修改属性的值。
 *
 * 为了避免当调用的方法不存在时产生错误 使用 __call()/__callStatic 方法来避免 程序仍会继续执行下去。
 * __call 当要调用的方法不存在或权限不足时,会自动调用__call 方法。
 * __callStatic 当调用的静态方法不存在或权限不足时,会自动调用__callStatic方法。
 * __autoload 提供一种“lazy load”的机制,当第一次需要使用相关类时调用,这样就不会加载不必要的类 造成资源浪费
 */

//__clone
class Account {
	public $balance;

	public function __construct($balance) {
		$this->balance = $balance;
	 }
}

class PersonC {
	private $id;
	private $name;
	private $age;
	public $account;

	public function __construct($name, $age, Account $account) {
		 $this->name = $name;
		 $this->age = $age;
		 $this->account = $account;
	}

	public function setId($id) {
		$this->id = $id;
	}

	public function __clone() {
		 $this->id = 0;
		 $this->account = clone $this->account;
		 //如果不加这一句,account在clone是会只被复制引用,其中一个account的balance被修改另一个也同样会被修改
	}
}

$person = new PersonC("peter", 15, new Account(1000));
$person->setId(1);
$person2 = clone $person;
$person2->account->balance = 250;
var_dump($person, $person2);


//__call()||__callStatic();
class Member {
	public function __call($fnName, $fnParam) {
		echo $fnName .$fnParam[0] ."<br/>";
	}
	public function __callStatic($fnName, $fnParam) {
		echo $fnName .$fnParam[0] ."<br/>";
	}

}
$user = new Member();
$user->callFn(2);
Member::staticFn(1);

//__autoload();
function  __autoload($className) {
	$filePath = "{$className}.php";
	echo $filePath ."<br/>";
    if (is_readable($filePath)) {
			require($filePath);
		}
}
if (TRUE) {
	$a = new AutoLoadTest();
	$a->test();
	$b = new AutoLoadTest2();
	$b->test();
	// … 业务逻辑
} else {
	$a = new D();
	$b = new F();
	// … 业务逻辑
}

?>


<?php
/*
 * reflection 反射 获取某对象的所有信息
 * getProperties() 默认获取所有属性,返回数组;可选参数:
 * ReflectionProperty::IS_STATIC,
 * ReflectionProperty::IS_PUBLIC,
 * ReflectionProperty::IS_PROTECTED,
 * ReflectionProperty::IS_PRIVATE;
 * 在通过getName()得到单个属性名;
 * 通过getDocComment()可以得到写给property的注释;
 *
 * getMethods() 来获取到类的所有methods。返回数组
 *
 */
class Person {
	const constAttributes = "const";
	private $privateAttributes = "private";
	protected $protectedAttributes = "protected";//我是注释
	public $publicAttributes = "public";

	private function myPrivateFn() {
		return $this->privateAttributes = "myPrivate";
	}
	protected function myProtectedFn() {
		return $this->protectedAttributes = "myProtected";
	}
	public function myPublic($name) {
		return $this->publicAttributes = "myPublic";
	}
	public static function staticFn() {
		echo "staticFn<br/>";
	}
}

$class = new ReflectionClass("Person");
$properties = $class->getProperties();
$properties2 = $class->getProperties(ReflectionProperty::IS_PUBLIC);
foreach($properties as $propertie) {
	echo $propertie->getName() ."<br/>";
	echo $propertie->getDocComment() ."<br/>";
}

$methods = $class->getMethods();
foreach($methods as $method) {
	echo $method ."<br/>";
}

?>


 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值