php学习笔记9-(OOP)

本文深入介绍了PHP面向对象编程(OOP)的基本概念与实践,包括类的定义、接口使用、继承与抽象类等核心特性,并通过具体示例展示了如何实现封装、继承与多态等面向对象编程原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值