【问题标题】:PHP object attributes holding unexpected values包含意外值的 PHP 对象属性
【发布时间】:2018-05-11 06:00:58
【问题描述】:

所以我正在为一个学校项目开发一个通知系统,目前我被一个错误所困扰。创建通知时,会创建具有四个属性的 Notification 类的实例,其中之一是 unix 时间戳。然后我将这个对象传递给一个将四个属性插入数据库表的过程。问题是由于某些奇怪的原因,四个属性中的三个都包含 unix 时间戳,包括 unix 属性,而第四个仍然为空。回显不同阶段的属性表明问题在于何时将通知对象传递给将其插入表的过程。

创建通知类实例并将其传递给将其插入数据库的过程的过程。

require_once('notification.php');
require_once('database.php');

class NotificationStatic{

//Creates a new notification object and inserts it into the database
public static function CreateNotification($notificationType, $notificationUid, $notificationUidInvolved){
    $notification = new Notification($notificationType, $notificationUid, 
    $notificationUidInvovled, time());
    Database::AddNotification($notification);
}

通知对象及其构造函数

class Notification{
    public $notificationType;
    public $notificationUid;
    public $notificationUidInvolved;
    public $notificationUnix;

  function __construct($_notificationType, $_notificationUid, $_notificationUidInvolved, $_notificationUnix){
      $this->$notificationType = $_notificationType;
      $this->$notificationUid = $_notificationUid;
      $this->$notificationUidInvolved = $_notificationUidInvolved;
      $this->$notificationUnix = $_notificationUnix;
  }

}

最后程序将对象插入到数据库中

//Inserts notification into the database
public static function AddNotification($notification){
    self::Connect();
    $notificationType = $notification->$notificationType;
    $notificationUid = $notification->$notificationUid;
    $notificationUidInvolved = $notification->$notificationUidInvolved;
    $notificationUnix = $notification->$notificationUnix;
    $sql = "INSERT INTO notifications(notification_id, notification_type 
            ,notification_uid,
             notification_uid_involved, notification_unix)
             VALUES (DEFAULT, '$notificationType', '$notificationUid',
            '$notificationUidInvovled', '$notificationUnix')";
    self::$conn->query($sql);
    self::$conn->close();

}

提前感谢任何可以帮助我的人!

【问题讨论】:

  • notificationUidInvovled...
  • ->$ 中的美元符号(> 符号之后)都是错误的。但我无法拼凑为什么它们会导致您描述的行为。

标签: php mysql object mysqli


【解决方案1】:

$notification->$notificationUid 应该是 $notification->notificationUid

在您的构造函数和 addNotification 函数中..

其他属性相同

【讨论】:

    猜你喜欢
    • 2012-07-30
    • 1970-01-01
    • 2016-11-02
    • 2021-11-14
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多