XOOPS: Tutorial: Inheritance of xoopsForm

Posted by: MambaOn 2012/3/7 2:10:00 7057 reads
French 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. Resized Image Note: if we want our form to be no longer displayed in tabular form, we would inherit directly from XoopsForm and rework our source code; For here, it will be enough to just inherit from xoopsThemeForm and change the render() method which is responsible for creating the form. As you may have noticed, in XoopsForm (see API) the render() method is empty, and that's correct. This is an abstract class, and it just tells us that this method should be implemented in the class that extends it. So in XoopsThemeForm the method is implemented to display the form as a table:
function render()
{
    
$ele_name $this->getName();
    
$ret      '$ele_name '" id="' $ele_name '" action="' $this->getAction() . '" method="'
        
$this->getMethod() . '" onsubmit="return xoopsFormValidate_' $ele_name '();"' $this->getExtra() . '>
            
            
$this->getTitle() . '
        '
;
    
$hidden   '';
    
$class    'even';
    foreach (
$this->getElements() as $ele) {
        if (!
is_object($ele)) {
            
$ret .= $ele;
        } else {
            if (!
$ele->isHidden()) {
                if (!
$ele->getNocolspan()) {
                    
$ret .= '';
                    if ((
$caption $ele->getCaption()) != '') {
                        
$ret
                            
.=
                            
'. ($ele->isRequired() ? '-required' '') . '">';
                        
$ret .= '$caption ''
;
                        
$ret .= '*'
;
                        
$ret .= '
';
                    }
                    if ((
$desc $ele->getDescription()) != '') {
                        
$ret .= '$desc '