I’m implementing a PHP (5.4.4) class for including class definitions (in a dynamic fashion) for HTML rendering, so if I have a form with two buttons, four text inputs and a checkbox, I just include the .php files corresponding to a form, a textbox and a button, and not anything else (each HTML object is represented with a class).
But I have a problem… I created a class called ComponentManager for managing the loading process, with this code:
class ComponentManager {
// component manager properties
protected $components;
// constructor for this object
public function __construct() {
if (!empty($_SESSION['components'])) {
$this->components = explode(" ", $_SESSION['components']);
} else {
$this->components = null;
}
}
// destructor for this object
public function __destruct() {
$this->components = null;
}
// getter for this object
public function __get($property) {
if ($property === "components") {
return $this->components;
}
}
// addComponents - add components to the current components list
public function addComponents($components) {
if (!empty($components)) {
$list = explode(" ", $components);
$count = sizeof($list);
for ($i = 0; $i < $count; $i++) {
if (!in_array($list[$i], $this->components)) {
$this->components[] = $list[$i];
$component = null;
The problem is I seem to be failing with the in_array() function and I don’t know why… I mean, I’ve used it a lot of times in the past for different things but keeps telling me this:
Warning: in_array() expects parameter 2 to be array, null given in D:\apache\htdocs\webapps\skeleton\assets\scripts\manager.php on line 34
I pass $components in my test code as a whitespace-separated list, like this:
$page->addComponents("form checkbox textbox button range number");
My intention is to specify: if the component list is not empty, give me an array with the entered components, and for each one of the components, insert it if and only if it’s not already present in the components array.
What am I doing wrong?
Obviously, your constructor initiates
$this->componentstonulland then you are trying to access that throughaddComponents().You probably meant to initiate it to
$this->components = array();so the list starts as an empty array, thus allowing you to usein_array()on it?