php 类-__,PHP类

这篇博客介绍了PHP中类的定义和使用,包括构造函数和析构函数的应用。通过示例展示了如何创建并实例化类,还提到了__autoload函数用于自动加载类文件。在实际操作中,创建了testOne类并打印了'begin',以及创建了testTwo类,设置了name属性并通过getName方法输出了'Mynameis:test'。
摘要由CSDN通过智能技术生成

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

类的定义

Class abc

{

//成员函数和变量

}

?>

使用实例

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

class testOne{

function __construct(){

echo "begin";

}

function __destruct() {

}

}

?>

类文件:/class/class.testTwo.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

function __autoload( $class_name) {

require_once "./class/class.".$class_name.'.php';

}

?>

文件:/index.php

require_once( "comm.config.php" );

$testOne = new testOne();

?>

输出结果:begin

文件:/index2.php

require_once( "comm.config.php" );

$testTwo = new testTwo();

$testTwo->setName( "test" );

echo $testTwo->getName();

?>

输出结果:connMy name is:test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值