duanpen9294 2017-11-27 21:49
浏览 25
已采纳

PHP对象属性包含意外值

So i'm working on a notification system for a school project and am currently baffled by a bug. When a notification is created an instance of a Notification class is created with four attributes, one of which is a unix timestamp. I then pass this object into a procedure which inserts the four attributes into a database table. The problem is for some bizarre reason three of four attributes all hold the unix timestamp including the unix attribute whilst the fourth remains empty. Echoing out the attributes at different stages showed that the problem is when the notification object is passed to the procedure which inserts it into the table.

The procedure which creates an instance of the notification class and passes it to the procedure which inserts it into the database.

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);
}

The notification object with it's constructor

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;
  }

}

And finally the procedure inserts the object into the database

//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();

}

Thanks in advance to anyone who can help me out!

  • 写回答

1条回答 默认 最新

  • duanmei2459 2017-11-27 21:57
    关注

    $notification->$notificationUid should be $notification->notificationUid

    in your constructor and in your addNotification function..

    the same for the other attributes

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效