【问题标题】:php Warning & Strict Standardsphp 警告和严格标准
【发布时间】:2015-12-15 02:29:20
【问题描述】:

我使用$_SESSION['']; 在用户登录后从他们那里获取值。这些数据来自users 表。

我正在尝试

`SELECT *` `FROM roster`

当用户 API=$API 时,通过查询并为每个值分配一个 var

我可以通过以下方式做到这一点:

error_reporting(E_ALL);

    include ('database_connection.php');
    $API        = $_SESSION['API'];

        if ($API !== false) { 
            $roster_query = "SELECT * FROM roster WHERE API='$API'";
            $result = mysqli_fetch_array(mysqli_query($dbc, $roster_query));

    /* ROSTERS TABLE */
            $StreetName     = $result['streetname'];
            $HouseNum   = $result['housenumber'];
            $City       = $result['city'];
            $State      = $result['state'];     
            $Zipcode        = $result['zipcode'];                               

    /* SESSION */
            $FirstName  = $_SESSION['firstname'];
            $LastName   = $_SESSION['lastname'];
            $Email      = $_SESSION['email'];

    /* this->$vars - Specific for this form */      
            $ExecutionDate  = date("Y:M:D");      
            $DOCSIGNEDBYIP  = $_SERVER['REMOTE_ADDR'];

        }

但是,当我这样做时,会出现以下错误。如果我删除上面的代码没有错误,等等。

现在这并没有“破坏”我的代码,但我不想把它扫到地毯下并稍后解决。

谁能确定我哪里出错了?

警告:在第 48 行的 /home/.../public_html/.../.../.../db.php 中从空值创建默认对象

第 48 行: $return->$i = $row;

if(!$return_array) {
    while($row = mysql_fetch_object($sql_query)) {
        $return->$i = $row;
        $i++;
    }

严格标准: 不应静态调用非静态方法 Access::is_logged(),假设 $this 来自 /home/.../public_html/.../ 中的不兼容上下文。 ../.../APP.php 第 64 行

pinAPP 第 64 行: return Access::is_logged();

public function is_logged()
    {
        return Access::is_logged();
    }

is_logged();

   <?php
    require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'init.php');

    class pinAPP {


        public function is_logged() {
            return Access::is_logged();
        }

    }

访问

<?php
    class Access {
        private static $auth = false;


        final public function is_logged( $require_admin_access = false ) {
            if ( ! isset($_SESSION[LOGINSESSION]) )
                return false;

            self::$auth = true;

            if ( $require_admin_access ) {
                $u = new User();
                if ( ! $u->is_admin() )
                    new Redirect(DEFAULT_RETURN_URL);
            }

            return self::$auth;
        }

    }

【问题讨论】:

  • 如果在循环之前执行$return = new stdClass();,则应该解析第 48 行。试试看。
  • 然后在Access类下,看is_logged()是否声明static function is_logged()用作Access::is_logged()。
  • 你介意发布一个描述更多细节的答案@Rasclatt

标签: php


【解决方案1】:

试试这些,看看它们是否有效:

// Set an empty object for $return first
$return =   new stdClass();

if( ! $return_array ) {
    $i = 0;
    while ( $row = mysql_fetch_object($sql_query) ) {
        $return->$i = $row;
        $i++;
    }

我认为另一个是说您应该将is_logged() 作为static 函数:

class Access
    {
        static function is_logged()
            {
                // code here
            }
    }

【讨论】:

  • 我的函数 is_logged() 已经在一个依赖于我的主 Web 应用程序的类中。对此的任何更改都会对我的网站造成严重破坏。有什么方法可以重组我的查询以适应我的 web 应用程序? @拉斯克拉特
  • 这也是第 64 行的错误 - 严格标准:非静态方法 Access::is_logged() 不应在 /home/.../public_html/../.. 中静态调用/../file.php 第 64 行 `
  • return_array 工作。谢谢,但是我在尝试使用 is_logged(); 时遇到了上面的错误^^^; @拉斯克拉特
  • 那么现在,只是方法导致了问题?
  • 是的,这就是问题所在。您必须将final public function is_logged( $require_admin_access = false) 转为final public static function is_logged( $require_admin_access = false)
【解决方案2】:
  1. 检查$row 使用!empty($row)(只是警告)
  2. 找不到static functon is_logged()

【讨论】:

  • 能否说得详细一点。
猜你喜欢
  • 2012-03-12
  • 2013-01-24
  • 2013-02-09
  • 2015-03-15
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
相关资源
最近更新 更多