Hi.
I'm following the very good article of Alejandro Gervasio
about The Singleton and Factory Patterns in PHP: Building a Form Generator Class
on PHP4.
On my own I tried to make this test:
<?php
class formElementFactory{
function formElementFactory()
{
var_dump($this);
///////
// return object(formElementFactory)#1 (0) { }
/////
}
function getInstance(){
static $instance;
if(!isset($instance)){
$object= __CLASS__;
$instance=new $object;
}
var_dump($this);
///////
// return NULL
/////
var_dump($instance);
///////
// return object(formElementFactory)#1 (0) { }
/////
return $instance;
}
function createElement($type,$className,$attributes=array())
{
if(!class_exists($className)||!is_array($attributes) || !is_string($type))
{
exit('Invalid method parameters');
}
// return a new form element object
$obj = new $className($type,$attributes);
return $obj->getHTML();
}
}
class addmodules
{
var $html;
function addmodules($type,$attributes=array())
{
if(count($attributes)<1)
{
exit('Invalid number of attributes');
}
$this->html='<input type="'.$type.'" ';
foreach($attributes as $attribute=>$value)
{
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
function getHTML(){
return $this->html;
}
}
$formFactory=formElementFactory::getInstance();
var_dump($formFactory);
///////
// return object(formElementFactory)#1 (0) { }
/////
$outPut = $formFactory->createElement("input",'addmodules',array('name'=>'fname','maxlength'=>'20'));
echo $outPut;
?>
I always knew that an object is a
class's instance so in my opion $this
IS a class's instance or not 😕 .
Why from inside the Constructor $this
return an object and than into the
getInstance method is NULL ?
Take care.