Blogger Information
Blog 55
fans 0
comment 0
visits 50509
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP-类的声明和实例化-0903
Bean_sproul
Original
703 people have browsed it

实例

<?php 

// 类声明与类的实例化;


/*用class 就能创建一个类*/
class lei{
   
  
}

//用关键字 new 实例化一个类
$lei = new lei();

//给当前对象添加一些属性
$lei->name = '小样';
$lei->sex = '男';

// 给当前对象添加一些方法
$lei->hello = function()
{
    return '我是一个自定义的类方法';
};

//使用对象访问符:-> 来访问对象中的成员(属性和方法)
echo $lei->name,': ',$lei->sex,'<br>';

//错误的调用方式,因为当前对象方法不是一个普通函数,而是一个对象方法echo $demo1->hello();

//正确的调用方式,对象方法应该以回调的方式运行
echo call_user_func($lei->hello);

?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

Correction status:qualified

Teacher's comments:这作业也太简洁了吧!下次写多点!!!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post