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';
# }
?>
#!/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';
# }
?>
相关文章推荐
- PHP学习笔记第【13】天(2014.11.16)——oop编程
- PHP 面向对象程序设计(oop)学习笔记 (二) - 静态变量的属性和方法及延迟绑定
- PHP 面向对象程序设计(oop)学习笔记 (五) - PHP 命名空间
- PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
- PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
- PHP 面向对象程序设计(oop)学习笔记(三) - 单例模式和工厂模式
- PHP面向对象(oop)的学习笔记
- PHP学习笔记-OOP
- PHP 面向对象程序设计(oop)学习笔记 (四) - 异常处理类Exception
- PHP 面向对象程序设计(oop)学习笔记 (五) - PHP 命名空间
- PHP 面向对象程序设计(oop)学习笔记 (四) - 异常处理类Exception
- php学习笔记--面向对象oop及注意点
- PHP 面向对象程序设计(oop)学习笔记(三) - 单例模式和工厂模式
- PHP 面向对象程序设计(oop)学习笔记 (二) - 静态变量的属性和方法及延迟绑定
- 我的php学习笔记(三十三)PHP中正则表达式学习及应用(一)
- PHP学习笔记
- PHP学习笔记【22】--PHP数据库编程 mysql扩展库 和mysqli扩展库
- web进阶之jQuery操作DOM元素&&MySQL记录操作&&PHP面向对象学习笔记
- PHP学习笔记——从搭建PHP开发环境开始
- PHP学习笔记 10 - 类