PHP PDO从其他类调用不同类中的公共方法

时间:2017-10-09 12:39:23

标签: php

我试图解决这个问题,我如何在另一个班级的班级中使用公共方法。我收到此错误

Site_id, PRP_Fabric, Phase12 {count}, Phase 13 {count}
6,       BORDB,        507,              507
6,       BORDG,        507,              0
6,       BORDG CHP2,   507,              507
6,       BORDO,        507,              0

我有包含insert方法的主类用户,基本上这个方法将值插入数据库。

try:
    import tkinter
    from tkinter import ttk
except ImportError:
    import Tkinter as tkinter
    import ttk


class Mainpage:

    def __init__(self, master):
        self.master = master
        self.frame = tkinter.Frame(self.master)
        self.master.columnconfigure(0, weight=1)
        self.master.columnconfigure(1, weight=3)
        self.master.columnconfigure(2, weight=1)
        self.master.columnconfigure(3, weight=1)
        self.master.columnconfigure(4, weight=1)

        self.searchfield = tkinter.Frame(self.master)
        self.searchfield.grid(row=1, column=0, columnspan=4)

        self.search_var = tkinter.StringVar()
        self.search_var.trace("w", lambda name, index, mode: self.selected)
        self.entry = tkinter.Entry(self.searchfield, textvariable=self.search_var, width=45)
        self.entry.grid(row=0, column=0, padx=10, pady=3)
        self.searchbtn = tkinter.Button(self.searchfield, text='Search', command=self.selected)
        self.searchbtn.grid(row=0, column=1)
        self.treeFrame = tkinter.Listbox(self.searchfield, width=45, height=150)
        self.treeFrame.grid(row=1, column=0, padx=10, pady=3)

        self.tree = ttk.Treeview( self.treeFrame, columns=('Status', 'Name', 'Date'))
        self.tree.heading('#0', text='Status')
        self.tree.heading('#1', text='Name')
        self.tree.heading('#2', text='Date')
        self.tree.heading('#3', text='Transact Code')
        self.tree.column('#0', stretch=tkinter.YES)
        self.tree.column('#1', stretch=tkinter.YES)
        self.tree.column('#2', stretch=tkinter.YES)
        self.tree.column('#3', stretch=tkinter.YES)
        self.tree.grid(row=0, columnspan=4, sticky='nsew')
        self.treeview = self.tree

        self.i = 1
        self.patient_list = [{"Name":"Harry Thuku", "Date":"07.09.2017"},
                             {"Name":"stella stl", "Date": "07.09.2017"},
                             {"Name":"geg be", "Date": "07.09.2017"},
                             {"Name":"Henry Reeta", "Date": "06.09.2017"},
                             {"Name":"Henry Reeta", "Date": "06.09.2017"}]

        for p in self.patient_list:
            self.tree.insert('', 'end', text="ID_"+str(self.i), values=
            (p["Name"], p["Date"]), tags=p["Name"])
            self.i = self.i + 1

        self.search_item = self.entry.get()
        for p in self.patient_list:
            if p["Name"] == self.search_item:
                self.selected(self.search_item)

        self.frame.bind("<Button-3>", self.click)
        self.frame.grid_location(0, 0)


    def selected(self):
        search_for = self.search_var.get()
        iid_to_select = ()

        if search_for != '':
            all_tags = self.master.tk.call(str(self.tree), "tag", "names")
            tags_to_select = tuple(filter(lambda tag: search_for.lower() in tag.lower(), all_tags))
            for sorted_tag in tags_to_select:
                iid_to_select += self.tree.tag_has(sorted_tag)

        self.tree.selection_set(iid_to_select)

    def click(self, event):
        print("Clicked at: ", event.x, event.y)
        self.master.withdraw()
        self.toplevel = tkinter.Toplevel(self.master)
        self.toplevel.geometry("480x480")
        app = PatientPage(self.toplevel)


class PatientPage:
    #.....
    pass



root = tkinter.Tk()
root.title("Login")
root.geometry("480x480")
Mainpage(root)
root.mainloop()

这是我的config.php文件来实例化类,所以我可以使用它们

Fatal error: Uncaught Error: Using $this when not in object context

我还有其他课程消息,然后按照。在后续类中,我可以从用户类访问所有方法,在消息类中我尝试使用在用户类中的插入方法

<?php
  class User{
    protected $pdo;
    public function __construct($pdo){
      $this->pdo = $pdo;
    }

    public function insert($table, $fields){
      //here we insert the values into database
    }
  }
?>

在下面的课程中我尝试使用sendNotification方法,请记住此方法在消息类中,并且它使用来自用户类的方法

<?php
    session_start();
    include 'database/connection.php';
    include 'classes/user.php';
    include 'classes/follow.php';
    include 'classes/message.php';
    global $pdo;

    $userObj     = new User($pdo);
    $followObj   = new Follow($pdo);
    $messageObj  = new Message($pdo);
?>

致命致命错误

<?php 
    class Message extends User{
        public function __construct($pdo){
            $this->pdo  = $pdo;
        }

        public static function sendNotification($user_id, $followerID, $type){
            //calling this method from user class 
            $this->insert('notifications', array('By' => $user_id, 'To' => $followerID, 'type' => $type));
        }
    }
?>

我该怎么办?

2 个答案:

答案 0 :(得分:0)

您正尝试在此方法中使用静态方法访问$ this:

public static function sendNotification($user_id, $followerID, $type){
            //calling this method from user class 
            $this->insert('notifications', array('By' => $user_id, 'To' => $followerID, 'type' => $type));

您可以将sendNotification设为非静态并创建Message实例,以便您能够访问User的公共成员

答案 1 :(得分:0)

使$this成为非静态函数。删除$message = new Message($this->pdo);//pass database connection $message->sendNotification();//pass the variables 关键字

当方法不依赖于已实例化的类时,请使用静态方法。因此无法使用apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-database labels: app: my-database spec: replicas: 1 strategy: type: Recreate template: metadata: labels: app: my-database spec: containers: - image: postgres:9.6.3 name: my-database ports: - containerPort: 5432 resources: {} volumeMounts: - mountPath: "/var/lib/postgresql/data:Z" name: postgresdb-storage restartPolicy: Always volumes: - name: postgresdb-storage persistentVolumeClaim: claimName: postgresdb-volume-claim status: {} 的原因

在以下功能中,改为执行此操作

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgresdb-volume
spec:
  storageClassName: postgresdb-class
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 100Gi
  nfs:
    server: <NFS_SERVER_IP_ADDRESS>
    path: /var/nfs/data/my-database-data