Views: 11593
Last Modified: 09.09.2014

From version 12.0.0 and later, component class support is available. It is implemented in the form of a file /component_name/class.php. Class.php is a reserved file name, and this file is automatically connected upon retrieval:

$APPLICATION->IncludeComponent()

In this case, the final call of the method initComponent is made, where class.php (if any) is connected from which the most recent inheritor from CBitrixComponent is taken.

The actions like:

class CDemoTest extends CBitrixComponent{}
class CDemoTestDecorator1 extends CDemoTest {}
class CDemoTestDecorator2 extends CDemoTest {}

will not be successful. As a result, CDemoTestDecorator2 will be used.

Please note that when changing the base class of the component, the behavior of all its descendants (other components) will have to be taken into account.

Examples of Use

Let us consider the simplest component taking the square of a parameter.

File /bitrix/components/demo/sqr/component.php:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

$arParams["X"] = intval($arParams["X"]);
if($this->startResultCache())
{
    $arResult["Y"] = $arParams["X"] * $arParams["X"];
}
$this->includeComponentTemplate();
?>

File /bitrix/components/demo/sqr/templates/.default/template.php:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>
<div class="equation">
<?echo $arParams["X"];?> squared is equal to <?echo $arResult["Y"];?>
</div>

In the real components, there may be three dozen lines and 5-6 similar operations instead of the multiplication operation. As a result, the file component.php becomes difficult to understand.

Let us separate the component logic into a class.

File /bitrix/components/demo/sqr/class.php:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

class CDemoSqr extends CBitrixComponent
{
    //Parent method passes along all of the parameters transferred to $APPLICATION->IncludeComponent
    //and applies the function htmlspecialcharsex to them. In this case, such processing is excessive.
    //Redefine.
    public function onPrepareComponentParams($arParams)
    {
        $result = array(
            "CACHE_TYPE" => $arParams["CACHE_TYPE"],
            "CACHE_TIME" => isset($arParams["CACHE_TIME"]) ?$arParams["CACHE_TIME"]: 36000000,
            "X" => intval($arParams["X"]),
        );
        return $result;
    }

    public function sqr($x)
    {
        return $x * $x;
    }
}?>

File /bitrix/components/demo/sqr/component.php:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

if($this->startResultCache())
{
    //$this - an instance of CDemoSqr
    $arResult["Y"] = $this->sqr($arParams["X"]);
}
$this->includeComponentTemplate();
?>

Now the code in the fileе component.php has become controllable.

Component Inheritance

For example:

The file /bitrix/components/demo/double_sqr/class.php:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

//Necessary for the correct search of the class CDemoSqr
CBitrixComponent::includeComponentClass("demo:sqr");
//Inheritor that expands functionality:
class CDemoDoubleSqr extends CDemoSqr
{
    public function sqr($x)
    {
        return parent::sqr($x)*2;
    }
}?>

The file /bitrix/components/demo/double_sqr/component.php is identical to the file /bitrix/components/demo/sqr/component.php, the contents can be copied.

The file /bitrix/components/demo/double_sqr/templates/.default/template.php:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>
<div class="equation">
<?echo $arParams["X"];?> squared multiplied by 2 is equal to <?echo $arResult["Y"];?>
</div>

A Component without component.php

A component may also be created without the file component.php

To do so, it is sufficient to deactivate the method executeComponent. For example:

class CDemoSqr extends CBitrixComponent
{
...
    public function executeComponent()
    {
        if($this->startResultCache())
        {
            $this->arResult["Y"] = $this->sqr($this->arParams["X"]);
        }

        $this->includeComponentTemplate();

        return $this->arResult["Y"];
    }
};

Now, the files component.php can be deleted from both components.



Courses developed by Bitrix24