Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP.NET > php学习笔记9-(OOP)
【标  题】:php学习笔记9-(OOP)
【关键字】:php,9-,OOP
【来  源】:http://blog.csdn.net/yxf/archive/2006/10/07/1324410.aspx

php学习笔记9-(OOP)

Your Ad Here 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';
#
 }
?>

 
互联网信息的离散与聚合:【上一篇】
php学习笔记8-(定义函数):【下一篇】
【相关文章】
  • rh9 下的apache1.3.37+mysql5.0.22+php4.4.4 配置
  • kernel 2.6.19-Libata PATA (Parallel ATA) merge
  • 优化安装 mysql+apache+php
  • php学习笔记6-(控制台程序)
  • php学习笔记7-(数组操作)
  • php学习笔记5—(文件操作)
  • php学习笔记1—(a+b)
  • php学习笔记2—(字符串和注释)
  • php学习笔记3—(变量、常量和操作符)
  • php学习笔记4—(控制结构)
  • 【随机文章】
  • VLAN典型配置举例
  • 自定义Flash的DataGrid组件中字体的外观
  • 让Excel帮你查邮编
  • 传奇世界 超强升级武器秘诀
  • 存储过程从入门到熟练(多个存储过程完整实例及调用方法)
  • 如何:强化 TCP/IP 堆栈安全
  • 路由器公平排队仿真模型研究与实现
  • 智能家居布线
  • Linux bootloader 编写方法
  • Source Insight3.0: Linux源代码阅读的利器
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.