• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP 类的一些知识点

php 搞代码 3年前 (2022-01-23) 25次浏览 已收录 0个评论

1.类的定义

<?phpclass Cart{   var $items;   function add_item($artnr,$num){      $this->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. . . */    }}

类如果没有构造函数,将调用基类构造函数。

构造函数参数可以赋默认值

<?phpclass Constructor_Cart extends Cart {    function Constructor_Car@本文来源gaodai$ma#com搞$代*码6网搞代gaodaima码t($item = "10", $num = 1) {        $this->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 — 如果对象属于该类或该类是此对象的父类则返回 TRUEis_subclass_of — 如果此对象是该类的子类,则返回 TRUEmethod_exists — 检查类的方法是否存在property_exists — 检查对象或类是否具有该属性trait_exists — 检查指定的 trait 是否存在

5.继承

<?phpclass Named_Cart extends Cart {    var $owner;    function set_owner ($name) {        $this->owner = $name;    }}?>

PHP不支持多继承。
5.静态方法

<?phpclass A {    function example() {        echo "I am the original function A::example().
\n"; }}class B extends A { function example() { echo "I am the redefined function B::example().
\n"; 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();?>

6.基类引用 parent

<?phpclass A {    function example() {        echo "I am A::example() and provide basic functionality.
\n"; }}class B extends A { function example() { echo "I am B::example() and provide additional functionality.
\n"; parent::example(); }}$b = new B;// 这将调用 B::example(),而它会去调用 A::example()。$b->example();?>

7.序列化

<?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();?>

8.魔术函数 __sleep __wakeup

参考:

http://php.net/manual/zh/oop4.php

以上就介绍了PHP 类的一些知识点,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP 类的一些知识点

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址