您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP 类的一些知识点_PHP教程

2025/8/16 14:18:49发布15次查看
php 类的一些知识点 1.类的定义items[$artnr += $num; }}
不能将一个类分开定义在多个文件,也不能将类定义分到多个php块(函数内部可以分)。
不能定义名为以下的类:
stdclass
__sleep
__wakeup
事实上不要以__开头定义类。
2.构造函数class cart { var $todays_date; var $name; var $owner; var $items = array(vcr, tv); function cart() { $this->todays_date = date(y-m-d); $this->name = $globals['firstname']; /* etc. . . */ }}
类如果没有构造函数,将调用基类构造函数。
构造函数参数可以赋默认值
add_item ($item, $num); }}// 买些同样的无聊老货$default_cart = new constructor_cart;// 买些实在货...$different_cart = new constructor_cart(20, 17);?>
@new 可以抑制发生在构造函数中的错误。
3.类的使用$cart = new cart;$cart->add_item(10, 1);
类内部使用$this代表自身。
4.类相关函数__autoload — 尝试加载未定义的类
call_user_method_array — 调用一个用户方法,同时传递参数数组(已废弃)
call_user_method — 对特定对象调用用户方法(已废弃)
class_alias — 为一个类创建别名
class_exists — 检查类是否已定义
get_called_class — 后期静态绑定(”late static binding”)类的名称
get_class_methods — 返回由类的方法名组成的数组
get_class_vars — 返回由类的默认属性组成的数组
get_class — 返回对象的类名
get_declared_classes — 返回由已定义类的名字所组成的数组
get_declared_interfaces — 返回一个数组包含所有已声明的接口
get_declared_traits — 返回所有已定义的 traits 的数组
get_object_vars — 返回由对象属性组成的关联数组
get_parent_class — 返回对象或类的父类名
interface_exists — 检查接口是否已被定义
is_a — 如果对象属于该类或该类是此对象的父类则返回 true
is_subclass_of — 如果此对象是该类的子类,则返回 true
method_exists — 检查类的方法是否存在
property_exists — 检查对象或类是否具有该属性
trait_exists — 检查指定的 trait 是否存在
5.继承owner = $name; }}?>
php不支持多继承。
6.静态方法; }}class b extends a { function example() { echo i am the redefined function b::example().; a::example(); }}// a 类没有对象,这将输出// i am the original function a::example().a::example();// 建立一个 b 类的对象$b = new b;// 这将输出// i am the redefined function b::example().// i am the original function a::example().$b->example();?>
7.基类引用 parent; }}class b extends a { function example() { echo i am b::example() and provide additional functionality.; parent::example(); }}$b = new b;// 这将调用 b::example(),而它会去调用 a::example()。$b->example();?>
8.序列化one; } }// page1.php: include(classa.inc); $a = new a; $s = serialize($a); // 将 $s 存放在某处使 page2.php 能够找到 $fp = fopen(store, w); fwrite($fp, $s); fclose($fp);// page2.php: // 为了正常解序列化需要这一行 include(classa.inc); $s = implode(, @file(store)); $a = unserialize($s); // 现在可以用 $a 对象的 show_one() 函数了 $a->show_one();?>
9.魔术函数 __sleep __wakeup10.允许数组方式访问对象属性方法1function obj2array(obj){
return new arrayobject(obj, arrayobject::array_as_props);
}
这个方法比较简单,另一个方法要继承arrayaccess要复杂一点。
11.数组转对象 /** * 数组转对象 * @param unknown $e * @return void|stdclass */ public static function arraytoobject($e){ if( gettype($e)!='array' ) return; foreach($e as $k=>$v){ if( gettype($v)=='array' || gettype($v)=='object' ) $e[$k]=(object)arraytoobject($v); } return (object)$e; }
12 自己实现的序列化与反序列化用在redis时比较方便:
/** * 序列化对象,返回$json字符串 */ public static function serialize($model){ //return serialize($model); if(!$model)return '{}'; $json='{'; foreach($model as $key2=>$value2){ if($json!='{')$json.=','; $json.=$key2:$value2; } $json.='}'; return $json; } public static function unserialize($json){ $json=str_replace('{', '', $json); $json=str_replace('}','',$json); $array=explode(',', $json); $result=[]; foreach($array as $key =>$value){ $temparr=explode(',',$value); $temparr1=explode(':',$temparr[0]); if(count($temparr1)==0)continue; $result[$temparr1[0]]=trim( $temparr1[1],''); } //$obj= (object)($result); return obj2array($result); //return $result; }
http://www.bkjia.com/phpjc/1027126.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1027126.htmltecharticlephp 类的一些知识点 1.类的定义 items[$artnr += $num; }} 不能将一个类分开定义在多个文件,也不能将类定义分到多个php块(函数内部可以分)。...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product