分享

PHP类

 天中仙 2012-02-23

类是一个独立的程序单位,是具有相同属性和方法的一组对象的集合。

类的定义

<?php 
Class abc  
{  
//成员函数和变量 
} 
?>  

使用实例

类文件:/class/class.testOne.php

<?php
class testOne{
    function __construct(){
        echo "begin";
    }
   
    function __destruct() {
    }
}
?>

类文件:/class/class.testTwo.php
<?php
class testTwo{
    protected $name;
    function __construct(){
        echo "conn";
    }
   
    function setName( $name ){
        $this->name = $name;
    }

    function getName(){
        return "My name is:".$this->name;
    }

    function __destruct() {
    }
}
?>

文件:/comm.config.php
<?php
function __autoload( $class_name) {
    require_once "./class/class.".$class_name.'.php';
}
?>

文件:/index.php
<?php
require_once( "comm.config.php" );
$testOne = new testOne();
?>
输出结果:begin

文件:/index2.php
<?php
require_once( "comm.config.php" );
$testTwo = new testTwo();
$testTwo->setName( "test" );
echo $testTwo->getName();
?>
输出结果:connMy name is:test

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多