duanhan4763 2011-03-12 20:17
浏览 10
已采纳

静态属性| 与'this'和'self'的关系

Why is a static designation requierd to make this interpret correctly? See comment in code below.

<?php

require_once 'class.database.php';

class table extends database
{   
    static protected $_args=array(); // static added here to remove error   
    function __construct()
    {
        parent::__construct();
        self::protect();
    }
    function protect()
    {
        foreach($_POST as $key => $value)
        {
            self::$_args[$key] = mysql_real_escape_string($value);
        $i++;
        }
    }

. . .

  • 写回答

1条回答 默认 最新

  • 普通网友 2011-03-12 21:10
    关注

    There are class properties and object properties. Class properties belong to the class itself, not an instance, are declared with static, and are accessed with self::. Object properties belong to an instance of the class, are declared without static, and are accessed with $this->.

    You can access a class property from an object method, if you use self::. But you'll be accessing the same property that other instances access.

    You can't access an object property from a static method, of course, since there's no object/instance involved.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数