I’m primarily a JS developer, using PHP for that server-side kick and I’m looking to see if there’s an method in PHP similar to the JS:
var something = {
action1: function() { alert('Fire 1!'); },
action2: function() { alert('Fire 2!'); },
action3: function() { alert('Fire 3!'); }
}
Then allowing me to call like:
something.action2(); // Alerts 'Fire 2!'
It’s for a relatively small app I’m working on and I’d like to avoid building classes. Since PHP is such a small part I’d like to keep it as similar in code-style as possible.
PHP does not have the same form of objects that JavaScript does, as PHP is a class-based language, while JavaScript is a prototype-based language. What this essentially means is that PHP creates objects by instantiating a class. JavaScript, however, has objects that are then cloned to create new objects.
If you wish to keep your code understandable, I’d recommend using PHP classes: