我只是想确保我正确地做到了这一点。我在$this->parameter
,$parameter
和public $parameter
这是我正在做的一个例子。我希望能够使用$instance->parameter
方法访问任何声明为public的内容。这是否意味着点访问也会起作用?我是多余的吗?
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
}
}
答案 0 :(得分: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