How is $event used in "Logic hooks"?

I see this example code that contains a method called StampIt that has 2 arguments $focus and $event

//prevents directly accessing this file from a web browser
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class AddTimeStamp
        function StampIt(& $focus, $event)
            global $current_user;
            $focus->description .= "Saved on ". date("Y-m-d g:i a"). " by ". $current_user->user_name;         }

I’m just trying to understand where $event argument is used since it’s not mentioned anywhere inside the StampIt method

Thank you

It’s in the Documentation:

Logic hook functions follow a pre-set format, with regular parameters (event, bean, focus) depending on the kind of hook it is. You don’t have to use the parameters, often you don’t, but they have to be there to match the pre-set format.

By the way, when I’m trying to understand code I save a lot of time by looking at the values of variables, stepping through code with a debugger, or just dumping variables into the logs, instead of asking in Forums :slight_smile:

The code was found here

If you can point me to where I can look for the “values of variables” on that page to see what the value of the $event is then I’d love to see it…

with the link you supplied it makes more sense now, but it’s not like i’m looking at the complete script and just too lazy to do a CTRL+F to see what the definition of $event is…

When you are developing, it’s a good idea to install a IDE (like Eclipse of PHP Storm, or Developer Studio on Windows) and then you can use the debugger to inspect values in runtime.

If you don’t have that, you can intersperse debugging commands in the code to echo variables on to the logs.

You can also use “find in files” linux commands, or use text editors with that function, to search for the place where variables and classes are defined.

1 Like

appreciate the tips! in this case I got another reply from stackoverflow and they indicated that this value is simply not used at all inside the method, so that’s what was confusing me.


$event will contain the name of the currently executing event/hook type during runtime. This is useful if a single method is hooked into more than one event and needs to alter its logic based on what event triggered it.

In this particular example we know that $event will always contain the string before_safe as this is the only event our example method has been hooked into. No other event will ever call that method.

That is why the variable remains unused in this example, as the method does not have to handle more than that single event type anyway and would also not gain any new information by reading $event.