Home > Backend Development > PHP Tutorial > Some knowledge points about PHP classes

Some knowledge points about PHP classes

WBOY
Release: 2016-08-08 09:28:05
Original
1027 people have browsed it

1. Class definition

<?php
class Cart{
   var $items;
   function add_item($artnr,$num){
      $this->items[$artnr += $num;
   }
}
Copy after login

A class cannot be defined in multiple files, nor can the class definition be divided into multiple PHP blocks (it can be divided inside a function).

You cannot define a class named:

stdClass

__sleep

__wakeup

In fact don’t define a class starting with __.

2. Constructor

If the

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. . . */
    }
}
Copy after login
class does not have a constructor, the base class constructor will be called.

Constructor parameters can be assigned default values

<?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);
?>
Copy after login

@new can suppress errors that occur in the constructor.
3. Usage of classes

$cart = new Cart;
$cart->add_item("10", 1);
Copy after login
class uses $this internally to represent itself.

4. Class related functions

__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 是否存在
Copy after login
5. Inheritance

<?php
class Named_Cart extends Cart {
    var $owner;

    function set_owner ($name) {
        $this->owner = $name;
    }
}
?>
Copy after login
PHP does not support multiple inheritance.
5. Static method

<?php
class A {
    function example() {
        echo "I am the original function A::example().<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am the redefined function B::example().<br />\n";
        A::example();
    }
}

// A 类没有对象,这将输出
//   I am the original function A::example().<br />
A::example();

// 建立一个 B 类的对象
$b = new B;

// 这将输出
//   I am the redefined function B::example().<br />
//   I am the original function A::example().<br />
$b->example();
?>
Copy after login

6. Base class reference parent

<?php
class A {
    function example() {
        echo "I am A::example() and provide basic functionality.<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am B::example() and provide additional functionality.<br />\n";
        parent::example();
    }
}

$b = new B;

// 这将调用 B::example(),而它会去调用 A::example()。
$b->example();
?>
Copy after login
7. Serialization

<?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();
?>
Copy after login
8. Magic function __sleep __wakeup

Reference:

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

The above introduces some knowledge points of the PHP class, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template