XOOPS: Tutorial: Inheritance of xoopsForm
Posted by: MambaOn 2012/3/7 2:10:00 7266 readsFrench XOOPS user br_750 recently published a nice article about inheritance of xoopsForm class. Here is the English translation:
What is inheritance?
Inheritance is a concept specific to Object-Oriented Programming (OOP), to create a new class based on an existing class. The new class "inherits" the properties and methods of the class it inherits from, called "parent" class. The inheriting class is called a "child" class.
This definition is very short and you can enhance your knowledge of object-oriented programming on the Web:
- Wikipedia - French article with nice UML images - Tutorial for PHP - PHP docs XOOPS and Inheritance Like any good IT project, XOOPS uses OOP. Indeed its developers (whom I thank by the way) wrote a set of classes that govern this project. So we can use inheritance to add and modify the behavior of XOOPS. All this is fine, but why? - you can modify any classes directly in the project. These changes are called " HACKS ". At the very moment you enter your code, it might become a grain of sand that might undermine your life forcing you to constant development and maintenance of your site. And what will happen in the next release arriving in few days? In a year? Nothing is going to run correctly, you'll have to start all changes one by one. Let's hope that as a competent developer, you have recorded all changes somewhere to be able to repeat them again, if needed. Otherwise I wish you good luck! The alternative is to use inheritance, let's go! Consider an example with a form in XOOPS. Forms in XOOPS, though often enough, can not always meet all your needs. Here we will see two applications of inheritance with XOOPS and more specifically with xoopsThemeForm and xoopsElement. These are only examples and you're not limited to them: - change the URL of the form's action on the fly - add a new item, not available in the original project We assume that we have a module called MyModule, and that this module has a standard directory for classes in which we can place our new classes. The first thing is to understand how classes are structured in form inheritance tree. XoopsForm class that is the base class of the form, is an abstract class, which means that this class should not be used directly, but must be extended and generalized. This is done in a kind of template, properties and methods. I let you immerse yourself in OOP for more details. XoopsThemeForm class is the class that extends XoopsForm and that'll display the form as a table. It is this a class which we inherit, to change the general behavior of the form.

function render()
{
$ele_name = $this->getName();
$ret = '' . NWLINE;
$ret .= $this->renderValidationJS(true);
return $ret;
}
// create form object
$my_form = new ThemeForm("my form", 'Form_bien', "handle_bien.php?action=save");
// create form elements
$reference = new XoopsFormText("Reference", "reference", 50, 100);
$prix= new XoopsFormText("Price","price",50,100);
// add elements to the form
$my_form->addElement($reference,true);
$my_form->addElement($price, true);
// create/add buttons
$button = new XoopsFormButton('', 'post', _SEND, 'submit');
$my_form->addElement($button);
// display the form
$my_form->display();

class MyForm extends XoopsThemeForm{
}
?>
include_once(./../XoopsThemeForm.php)
class MyForm extends XoopsThemeForm{
}
?>
defined('XOOPS_ROOT_PATH') or die('Restricted access');
xoops_load('XoopsThemeForm');
class MyForm extends XoopsThemeForm {}
xoops_load('XoopsThemeForm');
class MyForm extends XoopsThemeForm {
private $newAction;
public function setNewAction($url=null){
$url===null?$this->newAction=$this->getAction(): $this->newAction=$action;
}
}
?>
xoops_load('XoopsThemeForm');
class MyForm extends XoopsThemeForm {
private $newAction;
public function setNewAction($url=null){
$url===null?$this->newAction=$this->getAction(): $this->newAction=$action;
}
function render()
{
$ele_name = $this->getName();
$ret = '' . NWLINE;
$ret .= $this->renderValidationJS(true);
return $ret;
}
}
?>
$my_form=new MyForm(parameter);
If(condition){
// Now we change the action URL of the form
$MyForm->setNewAction('test.php')
}

defined('XOOPS_ROOT_PATH') or die('Restricted access');
class MyElement extends XoopsFormElement
{
var $_content;
function __construct($caption = '', $value = '', $name = '')
{
$this->setCaption($caption);
$this->setName($name);
$this->_value = $value;
}
function setContent($content)
{
$this->_content=$content;
}
function getContent($encode=false){
return $encode ? htmlspecialchars($this->_content, ENT_QUOTES) : $this->_content;
}
function render()
{
return $this->getContent();
}
}
?>
$planchePhoto=new myElement();
$planchePhoto->setContent('photos');
$my_form->addElement($planchePhoto);