我只是想確保我正確地做到了這一點。我有點困惑$this->parameter
,$parameter
和public $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
}
}
在PHP中,dot是用於字符串連接的。如果你想訪問對象的方法或成員,你必須在你的實例上使用' - >',然後使用方法或公共成員的名字 – MatRt
AH!所以方法也需要' - >'!很有幫助! ' - >'是否只訪問被聲明爲公共的成員? – Pinwheeler
是的。 http://php.net/manual/en/language.oop5.visibility.php – ficuscr