登录  /  注册

PHP 类的一些知识点_PHP教程

php中文网
发布: 2016-07-13 09:47:16
原创
764人浏览过

PHP 类的一些知识点

1.类的定义

<code class="hljs" xml=""><!--?php
class Cart{
   var $items;
   function add_item($artnr,$num){
      $this--->items[$artnr += $num;
   }
}</code>
登录后复制

不能将一个类分开定义在多个文件,也不能将类定义分到多个PHP块(函数内部可以分)。
不能定义名为以下的类:
stdClass
__sleep
__wakeup
事实上不要以__开头定义类。

2.构造函数

<code class="hljs" php="">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[&#39;firstname&#39;];
        /* etc. . . */
    }
}</code>
登录后复制

类如果没有构造函数,将调用基类构造函数。
构造函数参数可以赋默认值

<code class="hljs" xml=""><!--?php
class Constructor_Cart extends Cart {
    function Constructor_Cart($item = 10, $num = 1) {
        $this--->add_item ($item, $num);
    }
}

// 买些同样的无聊老货
$default_cart = new Constructor_Cart;
// 买些实在货...
$different_cart = new Constructor_Cart(20, 17);
?></code>
登录后复制

@new 可以抑制发生在构造函数中的错误。

3.类的使用

<code class="hljs" lasso="">$cart = new Cart;
$cart->add_item(10, 1);</code>
登录后复制

类内部使用$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.继承

<code class="hljs" php=""><!--?php
class Named_Cart extends Cart {
    var $owner;

    function set_owner ($name) {
        $this--->owner = $name;
    }
}
?></code>
登录后复制

PHP不支持多继承。

6.静态方法

<code class="hljs" php=""><!--?php
class A {
    function example() {
        echo I am the original function A::example().<br /-->
;
    }
}

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();
?></code>
登录后复制

7.基类引用 parent

<code class="hljs" php=""><!--?php
class A {
    function example() {
        echo I am A::example() and provide basic functionality.<br /-->
;
    }
}

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();
?></code>
登录后复制

8.序列化

<code class="hljs" php=""><!--?php
// classa.inc:
  class A {
      var $one = 1;

      function show_one() {
          echo $this--->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();
?></code>
登录后复制

9.魔术函数 __sleep __wakeup

10.允许数组方式访问对象属性

方法1

function obj2array(obj){
return new ArrayObject(obj, ArrayObject::ARRAY_AS_PROPS);
}
这个方法比较简单,另一个方法要继承ArrayAccess要复杂一点。

11.数组转对象

<code class="hljs" php="">    /**
     * 数组转对象
     * @param unknown $e
     * @return void|StdClass
     */
    public static function arrayToObject($e){
        if( gettype($e)!=&#39;array&#39; ) return;
        foreach($e as $k=>$v){
            if( gettype($v)==&#39;array&#39; || getType($v)==&#39;object&#39; )
                $e[$k]=(object)arrayToObject($v);
        }
        return (object)$e;
    }</code>
登录后复制

12 自己实现的序列化与反序列化

用在redis时比较方便:

<code class="hljs" php="">    /**
     * 序列化对象,返回$json字符串
     */
    public static function serialize($model){
        //return serialize($model);
        if(!$model)return &#39;{}&#39;;
        $json=&#39;{&#39;;
        foreach($model as $key2=>$value2){
            if($json!=&#39;{&#39;)$json.=&#39;,&#39;;
            $json.=$key2:$value2;
        }
        $json.=&#39;}&#39;;


        return $json;
    }
    public static function unserialize($json){
        $json=str_replace(&#39;{&#39;, &#39;&#39;, $json);
        $json=str_replace(&#39;}&#39;,&#39;&#39;,$json);
        $array=explode(&#39;,&#39;, $json);
        $result=[];
        foreach($array as $key =>$value){
            $temparr=explode(&#39;,&#39;,$value);
            $temparr1=explode(&#39;:&#39;,$temparr[0]);

            if(count($temparr1)==0)continue;
            $result[$temparr1[0]]=trim( $temparr1[1],&#39;&#39;);
        }
        //$obj=  (object)($result);

        return obj2array($result);
        //return $result;
    }</code>
登录后复制

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1027126.htmlTechArticlePHP 类的一些知识点 1.类的定义 items[$artnr += $num; }} 不能将一个类分开定义在多个文件,也不能将类定义分到多个PHP块(函数内部可以分)。...
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号