如何在 PHP OOP 中设置通知消息

Posted

技术标签:

【中文标题】如何在 PHP OOP 中设置通知消息【英文标题】:How to setup Notification Messages in PHP OOP 【发布时间】:2018-03-01 10:08:52 【问题描述】:

我正在使用 php OOP 开发 CMS。在这个项目中,我添加了一个功能,网站管理员可以添加和管理其他管理员。为了添加管理员,我创建了这个名为 admin_new.php 的页面,如下所示:

    <?php 
if (isset($_POST['submit']))
    $username = $_POST['uname'];
    $email = $_POST['email'];
    $password = $_POST['pass'];
    $groups = $_POST['groups'];
    if($groups == "Main Admin")
        $level = 1;
    else if($groups == "Administrator")
        $level = 2;
    else if($groups == "Content Creator")
        $level = 3;
    else if($groups == "Social Media Manager")
        $level = 4;
    else
        $level = 5;
    
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) 
        $notice['email_validation'] = "The email that you have entered is not a valid one";
    else
        $registration = new Register();
        $notices = $registration->CheckUname($username,$email,$password,$groups,$level)->getNotices();
       

?>
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            Add New Admin
            <small>You can add new admin here</small>
        </h1>
        <ol class="breadcrumb">
            <li class="active">addnewadmin.php</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-6">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Required Information</h3>
                    </div>
                    <?php 
                    if(isset($notice['email_validation'])) 
                        echo "
                            <div class='alert alert-danger'>
                                <strong>Hey!</strong> ".$notice['email_validation'].".
                            </div>
                        ";
                    
                    if(isset($notice['username_exists'])) 
                        echo "
                            <div class='alert alert-danger'>
                                <strong>Hey!</strong> ".$notice['username_exists'].".
                            </div>
                        ";
                    
                    if(isset($notice['email_exists'])) 
                        echo "
                            <div class='alert alert-danger'>
                                <strong>Hey!</strong> ".$notice['email_exists'].".
                            </div>
                        ";
                    
                    if(isset($notice['success_message'])) 
                        echo "
                            <div class='alert alert-success'>
                                <strong>Hey!</strong> ".$notice['success_message'].".
                            </div>
                        ";
                    
                    ?>
                    <form role="form" method="POST" action="">
                        <div class="box-body">
                            <div class="form-group">
                                <label>User name</label>
                                <input type="text" class="form-control" placeholder="Enter username" name="uname" required>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Email address</label>
                                <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" name="email" required>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">Temporary password</label>
                                <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Enter password" name="pass" required>
                            </div>
                            <div class="form-group">
                                <label>Group admin</label>
                                <select class="form-control" name="groups">
                                    <option value="Main Admin">Main Admin</option>
                                    <option value="Administrator">Administrator</option>
                                    <option value="Content Creator">Content Creator</option>
                                    <option value="Social Media Manager">Social Media Manager</option>
                                    <option value="Analyst">Analyst</option>
                                </select>
                            </div>
                        </div>
                        <div class="box-footer">
                            Visit <a href="https://zite.pouyavagefi.com/documentation/types.php">admin types</a> documentation to know the differences between each admin.
                        </div>
                        <div class="box-footer">
                            <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
</div>

我在这个文件中使用的类叫做Register.class.php,包含这个:

    <?php 
class Register
   
    protected $notice = array();
    private $db;
    public function __construct()
    
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    
    public function CheckUname($username,$email,$password,$groups,$level)
    
        if(!empty($username)&&!empty($email)&&!empty($password)&&!empty($groups)&&!empty($level))
        
            $chk1 = $this->db->prepare("SELECT username FROM admins WHERE user_name= ?");
            $chk1->bindParam(1,$username);
            $chk1->execute();
            if($chk1->rowCount() == 1)
            
                $notice['username_exists'] = "Try different username";
                return $notice;
            else
                $chk2 = $this->db->prepare("SELECT email FROM admins WHERE email_address= ?");
                $chk2->bindParam(1,$email);
                $chk2->execute();
                if($chk2->rowCount() == 1)
                
                    $notice['email_exists'] = "The email address that you have entered is already exists in database";
                    return $notice;
                else
                    $this->NewAdmin($username,$email,$password,$groups,$level);
                    $notice['success_message'] = "New admin was successfully added";
                    return $notice;
                
            
        
    
    public function NewAdmin($username,$email,$password,$groups,$level)
    
        if(!empty($username)&&!empty($email)&&!empty($password)&&!empty($groups)&&!empty($level))
        
            $reg = $this->db->prepare("INSERT INTO admins (user_name, email_address, password_hash, group_admin, date_joined, admin_level) VALUES ( ?, ?, ?, ?, NOW(), ?)");
            $reg->bindParam(1,$username);
            $reg->bindParam(2,$email);
            $reg->bindParam(3,$password);
            $reg->bindParam(4,$groups);
            $reg->bindParam(5,$level);
            $reg->execute();
        
    
    public function getNotices()
    
        return $this->notice;
    

?>

基本上我要做的就是确保:

1- 数据库中不存在用户名

2- 用户没有输入数据库中已经存在的电子邮件

3- 如果一切顺利,则在表格中添加一个新行

当我运行它并用虚拟信息填写表单以检查 notices 如何出现时,我收到以下错误消息:

致命错误:未捕获的错误:调用数组中的成员函数 getNotices() admin_new.php:22

堆栈跟踪:addnewadmin.php(20): require_once()

1 main 在第 22 行的 admin_new.php 中抛出

admins_new.php 的第 22 行是这样的:

$notices = $registration->CheckUname($username,$email,$password,$groups,$level)->getNotices();

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

问题在于概念......什么是什么

$registration -> 类的实例注册 CheckUname($username,$email,$password,$groups,$level) -> 返回数组的函数 $notice

基本上你的方法CheckUname()返回$notices然后你调用 这个数组方法getNotices() 有问题..

如果你想获得$notices,你必须在$registration实例上调用公共方法。

$registration->CheckUname($username,$email,$password,$groups,$level);
$notices = $registration->getNotices()

$notices = $registration->CheckUname($username,$email,$password,$groups,$level);

【讨论】:

很好的答案,但现在的问题是,它不返回 $notices 你可以在这里发布一些日志,还是有一些逻辑错误? 基本没有错误出现,逻辑完美。但它只是不显示 $notice 消息 应该是错字...在 admin_new.php 你设置一次 $notice 另一次 $notices并且在错误打印中你有 if(isset($notice['username_exists']))... Rewrite $notices on just $notice 我把所有的notices都改成了notice,但是还是没有解决问题:(

以上是关于如何在 PHP OOP 中设置通知消息的主要内容,如果未能解决你的问题,请参考以下文章

当在docker容器中设置php时,如何在vscode中设置php可执行路径php.validate.executablePath?

如何在activemq中设置队列监控

如何在用户通知中设置重复频率[重复]

如何在 Swift 中设置推送通知

如何在swift中设置每周本地通知

如何在 ionic.io -push 通知中设置大图像