0

我只是想确保我做得正确。我有点困惑$this->parameter$parameterpublic $parameter

这是我正在做的一个例子。我希望能够使用该$instance->parameter方法访问任何被声明为公共的东西。这是否意味着点访问也可以工作?我是多余的吗?

class Email {
    public $textBody;
    public $attachments;
    public $subject;
    public $emailFolder;

    function __construct($header) {
        $this->head = $header;
        $this->attachments = [];
        preg_match('/(?<=Subject: ).*/', $this->head, $match);
        $this->subject = $match[0];
        if ($this->subject) {
            $this->subject = "(No Subject)";
        }
        preg_match('/(?<=From: ).*/', $this->head, $match);
        $this->fromVar = $match[0];
        preg_match('/(?<=To: ).*/', $this->head, $match);
        $this->toVar = $match[0];
        preg_match('/((?<=Date: )\w+,\s\w+\s\w+\s\w+)|((?<=Date: )\w+\s\w+\s\w+)/', $this->head, $match);
        $this->date = $match[0];
        $this->date = str_replace(',', '', $this->date);
        $this->textBody = "";
    }

    function generateParsedEmailFile($textFile) {
        if (!(file_exists("/emails"))) {
            mkdir("/emails");
        }

        $this->emailFolder = 

        $filePath = "/emails/".$this->subject.$this->date;
        while (file_exists($filePath)) {
            $filePath = $filePath.".1";
        }
             # ......code continues
    }
}
4

1 回答 1

1

不,.仅用于连接字符串。public变量(以及函数)将可以使用->操作符直接从“外部”访问,而protected变量private(和函数)将需要访问 getter 和 setter 函数。一个示例如下所示:

class MyClass {
    public $variableA;
    protected $variableB;

    public function setB($varB) {
        $this->variableB = $varB;
    }

    public function getB() {
        return $this->variableB;
    }
}

$object = new MyClass();

$object->variableA = "new Content"; // works
$object->variableB = "new Content"; // generates an error
$object->setB("new Content");       // works
于 2013-05-19T21:23:28.790 回答