您的位置:首页 > 编程语言 > PHP开发

php学习笔记9-(OOP)

2006-10-07 18:19 316 查看
php的OOP语法与Java非常相似……


#!/usr/bin/php


<?php




# OOP here is much similar with OOP in Java




# interface definition


interface IOperations {


function plus();


}




# class definition


# visibilities of the members in a class: private protected public(default)


class Operations implements IOperations


{


private $Fa; # if with a visibility specified, do not use 'var', for example: private $a


private $Fb;


var $c;




public function __set($name, $value) { // __set is a setter for what isn't a field


switch ($name) {


case 'a':


$this->Fa=$value;


break;


case 'b':


$this->Fb=$value;


break;


default:


echo "property $name doesn't exist ";


break;


}


echo "Setter invoked: $name=$value ";


}




public function __get($name) { // __get is a getter for what isn't a field


echo "Getter invoked: property: $name ";


switch ($name) {


case 'a':


return $this->Fa;


break;


case 'b':


return $this->Fb;


break;


default:


break;


}


}




// function __class() is used to call a method which not exists, it


// could be used for declare overloaded functions




public function plus() {


# return $Fa+$Fb; <-- this is wrong!!!


$this->c=$this->a+$this->b;


return $this->c;


}




# constructor


# in php5, __construct() is the default


# for more compability with previous versions, Operations() is also a constructor,


# but Operations() is the second constructor


public function __construct($a, $b) {


$this->Fa=$a;


$this->Fb=$b;


$this->c=0;


echo "Constructor called! ";


}




# destructor


# destructor will be automatically called for example a variable is out of scope


public function __destruct() {


echo "Destructor called! ";


}


}




echo "Stage 1: ";


$objA=new Operations(3,4);


echo $objA->plus()." ";


$objA->a=5;


$objA->b=6;


echo $objA->plus()." ";


echo $objA->c." ";


if ( $objA instanceof IOperations ) echo "true "; else echo "false ";


unset($objA);


echo " ";




# abstract class & inheritance


# a class with at least an abstract method is an abstract class, it cannot be instanced


abstract class ParentClass {


public function __construct() {


echo "ParentClass.__construct() ";


}


public abstract function saySomething();


public function sayHello() { // like Java, every method and field could be overriden


echo "ParentClass.sayHello() ";


}


public function __destruct() {


echo "ParentClass.__destruct() ";


}


}


class ChildClass extends ParentClass {


public function __construct() {


echo "ChildClass.__construct() ";


}


public function saySomething() {


echo "ChildClass.saySomething() ";


}


public final function sayHello() { // here, final is the same as Java


echo "ChildClass.sayHello() ";


}


public function __destruct() {


echo "ChildClass.__destruct() ";


}


}


echo "Stage 2: ";


$objB=new ChildClass();


$objB->sayHello();


$objB->saySomething();


unset($objB);


echo " ";




# static in class:


# const field ( *** CONST ONLY *** )


# static method


class StaticThing {


const PI=3.14;


private function __construct() {


echo "StaticThing.__construct() ";


}


public function doSomething() {


echo "StaticThing.doSomething() ";


}


public static function getStaticThing() {


$object=new StaticThing();


return $object;


}


public function __destruct() {


echo "StaticThing.__destruct ";


}


}


echo "Stage 3: ";


// $objC=new StaticThing(); <-- IT's WRONG!


echo StaticThing::PI." ";


StaticThing::getStaticThing()->doSomething();


echo "The end! "




# P.S.


# function __autoload($name) is invoked automatically when you would access a undeclared class


# it's a function only, not a method


# example:


# function __autoload($name)


# {


# include_once $name.'.php';


# }


?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: