INSIDE CODE and OUTSIDE CODE
<?php
class Item
{
/**
* This is INSIDE CODE because it is written INSIDE the class.
*/
public $label;
public $price;
}
/**
* This is OUTSIDE CODE because it is written OUTSIDE the class.
*/
$item = new Item();
$item->label = 'Ink-Jet Tatoo Gun';
$item->price = 49.99;
?>
Ok, that's simple enough... I got it inside and out. The big problem with this is that the Item class is COMPLETELY IGNORANT in the following ways:
* It REQUIRES OUTSIDE CODE to do all the work AND to know what and how to do it -- huge mistake.
* OUTSIDE CODE can cast Item properties to any other PHP types (booleans, integers, floats, strings, arrays, and objects etc.) -- another huge mistake.
Note: we did it correctly above, but what if someone made an array for $price? FYI: PHP has no clue what we mean by an Item, especially by the terms of our class definition above. To PHP, our Item is something with two properties (mutable in every way) and that's it. As far as PHP is concerned, we can pack the entire set of Britannica Encyclopedias into the price slot. When that happens, we no longer have what we expect an Item to be.
INSIDE CODE should keep the integrity of the object. For example, our class definition should keep $label a string and $price a float -- which means only strings can come IN and OUT of the class for label, and only floats can come IN and OUT of the class for price.
<?php
class Item
{
/**
* Here's the new INSIDE CODE and the Rules to follow:
*
* 1. STOP ACCESS to properties via $item->label and $item->price,
* by using the protected keyword.
* 2. FORCE the use of public functions.
* 3. ONLY strings are allowed IN & OUT of this class for $label
* via the getLabel and setLabel functions.
* 4. ONLY floats are allowed IN & OUT of this class for $price
* via the getPrice and setPrice functions.
*/
protected $label = 'Unknown Item'; // Rule 1 - protected.
protected $price = 0.0; // Rule 1 - protected.
public function getLabel() { // Rule 2 - public function.
return $this->label; // Rule 3 - string OUT for $label.
}
public function getPrice() { // Rule 2 - public function.
return $this->price; // Rule 4 - float OUT for $price.
}
public function setLabel($label) // Rule 2 - public function.
{
/**
* Make sure $label is a PHP string that can be used in a SORTING
* alogorithm, NOT a boolean, number, array, or object that can't
* properly sort -- AND to make sure that the getLabel() function
* ALWAYS returns a genuine PHP string.
*
* Using a RegExp would improve this function, however, the main
* point is the one made above.
*/
if(is_string($label))
{
$this->label = (string)$label; // Rule 3 - string IN for $label.
}
}
public function setPrice($price) // Rule 2 - public function.
{
/**
* Make sure $price is a PHP float so that it can be used in a
* NUMERICAL CALCULATION. Do not accept boolean, string, array or
* some other object that can't be included in a simple calculation.
* This will ensure that the getPrice() function ALWAYS returns an
* authentic, genuine, full-flavored PHP number and nothing but.
*
* Checking for positive values may improve this function,
* however, the main point is the one made above.
*/
if(is_numeric($price))
{
$this->price = (float)$price; // Rule 4 - float IN for $price.
}
}
}
?>
Now there is nothing OUTSIDE CODE can do to obscure the INSIDES of an Item. In other words, every instance of Item will always look and behave like any other Item complete with a label and a price, AND you can group them together and they will interact without disruption. Even though there is room for improvement, the basics are there, and PHP will not hassle you... which means you can keep your hair!