Y
Y
Yaroslav Alexandrov2017-04-26 22:04:36
PHP
Yaroslav Alexandrov, 2017-04-26 22:04:36

How to get the value of a variable from one class to another?

Please help with advice. In any way at me it is impossible to deduce value of a variable from one class in another. there are two handlers, they are working, everything is fine here. I need to display the value of $WORKEREMAIL from the MyAdmin class in the MyClass class. How to do it right?

// регистрируем обработчик для благодарностей
AddEventHandler("iblock", "OnAfterIBlockElementAdd", Array("MyAdmin", "OnAfterIBlockElementAddHandler"));

class MyAdmin
{
    // создаем обработчик события "OnAfterIBlockElementAdd"
    function OnAfterIBlockElementAddHandler(&$arFields)
    {
 if(!$arFields["RESULT"])
        return false;
        if ($arFields["IBLOCK_ID"] == 31)
        {
    $db_props = CIBlockElement::GetProperty(31, $arFields['ID'], "sort", "asc", Array("CODE"=>"USER"));

if ($ar_props = $db_props->Fetch())
    {
    $THANKSUSER=$ar_props['VALUE'];//получаем значение сотрудника, которому объявили благодарность
    }
$rsUser = CUser::GetByID($THANKSUSER); 
$arUser = $rsUser->Fetch(); 
$WORKEREMAIL=$arUser['EMAIL']; //получаем значение почты сотрудника, которому объявили благодарность
  AddMessage2Log('$WORKEREMAIL-'.$WORKEREMAIL);
{ $arIBlockElement = GetIBlockElement($arFields["ID"]);
                $arSend = array('TEXT');
$arrSite = 's1';
                CEvent::Send('THANKS',$arrSite,$arSend);
}
        }
    }
}
// регистрируем обработчик для добавления в список переменных шаблона
AddEventHandler("main", "OnBeforeEventAdd", array("MyClass", "OnBeforeEventAddHandler"));
class MyClass extends MyAdmin
{
    function OnBeforeEventAddHandler(&$event, &$lid, &$arFields)
    {
  $Object = MyAdmin::OnAfterIBlockElementAddHandler();
  AddMessage2Log(print MyAdmin::$WORKEREMAIL);
        $arFields["WORKER_MAIL"] = '';//тут нужно вывести значение $WORKEREMAIL
    $event = 'THANKS';
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Burlaka, 2017-04-27
@alexyarik

First , you have a confusion in the code
.
Secondly ( essentially ), based on the order of occurrence of events:
- first it will be called - then the event will come - which will call the function
What does this call branch have to do with:
&$eventALREADY equal 'THANKS', and therefore it does not need to be redefined (and you can even add a check so that all mail events are not cut);
&$arFieldswill contain a link to an array $arSendinto which you can place the desired variable. Use as you need and clean (or don't clean - fields that are not in the mail template will simply be ignored).
Third : I don't like this kind of logic ( or it's not obvious what you're trying to do ). If $WORKEREMAILconceived as one of the fields in the letter, you just need to take it into account in the mail template and transfer it immediately to without subsequent interception. Also, it's spelled wrong. This should be an array
$arSendkey-value .

S
Sergey Sosnovsky, 2017-04-26
@desiigner

I don’t know your framework, but I can suggest writing the $WORKEREMAIL variable to a property of the MyAdmin class, and so on. it is inherited by the MyClass class, then this property will also be in your class.
that is, in the OnAfterIBlockElementAddHandler method, add the line $this->WORKEREMAIL = $WORKEREMAIL; and in the OnBeforeEventAddHandler method take this property $this->WORKEREMAIL;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question