php - Proper implementation of object parameters -
i want make sure i'm doing correctly. i'm little confused between $this->parameter
, $parameter
, public $parameter
here's example of i'm doing. want that's declared public able accessed $instance->parameter
method. mean dot accessing work? being redundant?
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 } }
no, .
concatenating strings. public
variables (as functions) accessible directly "outside" using ->
operator, while protected
, private
variables (and functions) need getter , setter functions accessed. example this:
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 error $object->setb("new content"); // works
Comments
Post a Comment