This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




8年前关闭。




我收到一个错误提示



在尝试运行我的脚本时。我是 PHP 类的新手,想知道是否有人可以指出我的错误。这是该部分的代码。
<?php
class ac
  {
  public function authentication()
    {
    private $plain_username = $_POST['username'];
    private $md5_password = md5($_POST['password']);

    $ac = new ac();

最佳答案

您不在函数/方法中定义类属性(公共(public)/私有(private)/等)。你在类(class)的主体中做这件事。

class ac
{
    private $plain_username;
    private $md5_password;

    public function authentication()
    {
        $this->plain_username = $_POST['username'];
        $this->md5_password = md5($_POST['password']);
    }
}

//declare a class outside the class
$ac = new ac();
如果要在函数/方法中定义变量,只需在不使用public/private/protected的情况下声明它们
$plain_username = $_POST['username'];

关于php - 尝试在函数中使用私有(private)变量时出现错误代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2496053/

10-09 05:56