Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The approach seems right (Except you need to use $this-> to access the property), but: Do yourself a flavour and have a look at how getter and setter are normaly used. A getter should return something, while a setter sets something. If you need to do "something" internal, you should use another method, or even do it in the constructor, etc..</p> <pre><code>class Item{ public $itemid; public $itemattribute; function __construct($itemid){ $this-&gt;itemid = $itemid; } public function getItemId(){ return $this-&gt;itemid; } //option 1:) public function calculateAttributeValue(){ $this-&gt;itemAttribute = foo($this-&gt;itemid); } //option 2:) public function calculateAttributeValue($func){ $this-&gt;itemAttribute = $func($this-&gt;itemid); } //option 3:) (lazy loading) public function getItemAttribute(){ if ($this-&gt;itemAttribute == null){ $this-&gt;itemAttribute = foo($this-&gt;itemid); } return $this-&gt;itemAttribute; } //option 4 (eager loading) function __construct($itemid){ $this-&gt;itemid = $itemid; $this-&gt;itemAttribute = foo($this-&gt;itemid); } public function getItemAttribute(){ return $this-&gt;itemAttribute; } } </code></pre> <p>However it is a GOOD idea, to either use eager OR Lazy loading. Whenever you need to "call" another function in order to compute a value, BEFORE a getter will return a value, your design becomes sensitive to "calling the functions in the right order", which will be bad for large Classes, and even worse, if one calculation depends on another calculation done prior...</p> <p>Bad usagE:</p> <pre><code>$myClass = new Item(5); $myClass-&gt;CalulateA(); $myClass-&gt;CalculateB(); $myClass-&gt;getB(); </code></pre> <p>Better:</p> <pre><code>$myClass = new Item(5); //calculates A and B $myClass-&gt;getB(); </code></pre> <p>or</p> <pre><code>$myClass = new Item(5); $myClass-&gt;getA(); //calculates A. $myClass-&gt;getB(); //calculates B and A when required. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload