gpt4 book ai didi

PHP 命名空间 - 父类变量未正确传递给子类

转载 作者:行者123 更新时间:2023-12-03 21:02:46 27 4
gpt4 key购买 nike

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

我有一个父类 - class.db.php
它处理创建和删除表(激活和停用插件)并存储一些变量,如表名等。

然后我有一个 child 类 - 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';
}
}
?>

child 类 - 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 );
}
}
?>

我试过移除 child __construct方法一起似乎不起作用。

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

编辑

来自:

我还有另外两个类 - 一个用于 Ajax 调用(加载下一个用户类),一个用于用户交互/格式化从数据库返回的结果(登录、身份验证检查等)
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;
}
}
?>

最佳答案

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

关于PHP 命名空间 - 父类变量未正确传递给子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56352289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com