Constructor Simplicity
If your class DOES CONTAIN instance members (variables) that need to be set, then your class needs to be initialized... and you should use __construct() to do that.
<?php
class MyClassA {
public $data1, $data2;
public function __construct($mcd1, $mcd2) {
$this->data1 = $mcd1; // INITIALIZE $data1
$this->data2 = $mcd2; // INITIALIZE $data2
}
}
$obj1 = new MyClassA("Hello", "World!"); // INSTANTIATE MyClassA
$d1 = $obj1->data1;
$d2 = $obj1->data2;
?>
If your class DOES NOT CONTAIN instance members or you DO NOT want to instantiate it, then there is no reason to initialize it or use __construct().
<?php
class MyClassB {
const DATA1 = "Hello";
public static $data2 = "World!";
}
$obj1 = new MyClassB(); // INSTANTIATE MyClassB - NO error.
$d1 = $obj1::DATA1; // ERROR
$d2 = $obj1::data2; // ERROR
$d1 = MyClassB::DATA1; // ok
$d2 = MyClassB::$data2; // ok
?>
The fact that $obj1 is useless and cannot be used as a reference, is further evidence that MyClassB objects should not be instantiated. NOTICE that MyClassB does not use private members or functions to make it behave that way. Rather, it is the collective nature of all the class members + what ISN'T there.
----
Server IP: 69.147.83.197
Probable Submitter: 66.53.213.181
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/86260
Del: integrated -- https://master.php.net/note/delete/86260/integrated
Del: useless -- https://master.php.net/note/delete/86260/useless
Del: bad code -- https://master.php.net/note/delete/86260/bad+code
Del: spam -- https://master.php.net/note/delete/86260/spam
Del: non-english -- https://master.php.net/note/delete/86260/non-english
Del: in docs -- https://master.php.net/note/delete/86260/in+docs
Del: other reasons-- https://master.php.net/note/delete/86260
Reject -- https://master.php.net/note/reject/86260
Search -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.