PHP Namepsace-父类变量未正确传递给子类

时间:2019-05-29 02:27:42

标签: php wordpress

我正在尝试将名称空间实现到我为WordPress开发的插件中,并且遇到了一个看起来很奇怪的问题。父变量在子__construct方法中可用,但子类中没有其他任何方法。

我有一个家长班-class.db.php

负责创建和删除表(激活和停用插件)并存储一些变量,例如表名等。

然后我有一个子班-class.db.users.php

扩展了上面的父类并处理了User表的所有CRUD方法。

父类-class.db.php

<?php
namespace PUWP\DB\Functions;

class PU_dbFunctions{

    public $wpdb;
    public $collate;
    public $userTable;

    function __construct(){
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->collate = $this->wpdb->get_charset_collate();
        $this->userTable = $this->wpdb->prefix.'pu_users';
    }
}
?>

儿童班-class.db.users.php

<?php

namespace PUWP\DB\Functions\Users;

DBFunctions(); // Temporary Function to include DB file above.

use PUWP\DB\Functions\PU_dbFunctions as PUWPDB;

class PU_dbUsers extends PUWPDB {

    function __construct(){
        parent::__construct();
        var_dump( $this->wpdb );
        // returns as expected
    }
    public function returnUserByEmail( $email ){
        var_dump( $this->wpdb );
        var_dump( $this->userTable );
        // both return null
        return $this->wpdb->get_results( $this->wpdb->prepare( "SELECT * FROM `{$this->userTable}` WHERE `email`= %s", $email ), OBJECT );
    }
}
?>

我尝试一起删除似乎无效的子__construct方法。

我做错了什么?一切正常,直到我开始添加名称空间。任何帮助将不胜感激。

编辑

致电:

我还有另外两个类-一个用于Ajax调用(加载下一个用户类),另一个用于用户交互/格式化从db返回的结果(登录,身份验证等)

class.user.php     

DBUserFunctions();

use PUWP\DB\Functions\Users\PU_dbUsers as DBUsers;
use DateTime;
class PU_User extends DBUsers {
    // Class Methods
    public function login( $email , $password ){

        $user = $this->returnUserByEmail( $email );
        //rest of login code if user is returned
    }
    public function get_UserByEmail( $email ){

        $user = $this->returnUserByEmail( $id ); 
        if( is_array( $user ) && ! empty( $user ) ){
            $user = $this->formatUser( $user[0] );
            return $user;
        }

        return false;
    }
}
?>

class.ajax.php

<?php
// User Database is included before this file is included in the plugin.
namespace PUWP\Ajax;
use PUWP\Users\PU_User as PUWPUSERS;

class Ajax_Handler{

    private $pu_settings;
    public $PUusers;

    function __construct(){

        $this->PUusers = new PUWPUSERS;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

我设法通过添加: parent::__construct();进入 class.users.php 方法 __construct