Why php functions or classes cannot work like jquery/ javascript plugins?
For instance,
a jquery plugin,
(function($){
// Attach this new method to jQuery
$.fn.extend({
// This is where you write your plugin's name
popup: function(options) {
// Set the default values, use comma to separate the settings, example:
var defaults = {
widthPopup: 500,
absoluteLeft: '500px'
}
var options = $.extend(defaults, options);
var o = options;
var $cm = this.click(function(e){
...
return false;
});
}
});
})(jQuery);
and then here is how you can use the plugin,
$('.click-me').popup();
or
$('.click-me').popup({widthPopup:300,absoluteLeft:'50%' });
or
$('.click-me').popup({absoluteLeft:'50%' });
as for a php function,
function test($parameter_1 = 100, $parameter_2 = false) {
....
}
and you have to call the function like this,
echo test();
or
echo test($parameter_1 = 50, $parameter_2 = true);
or
echo test(10, true);
and it won’t work if you call the function like this,
echo test($parameter_2 = true);
Can you see what I find that php is arbitrary and a bit ‘falling behind’??
Or maybe there is some advanced level of php I haven’t learned yet??
What you are describing are two different things. In the JavaScript example, you’re passing associative array (hash, map, JSON, whatever …), but in PHP, you’re using named parameters.
So the matching code in PHP would be
and calling it via
it’s not as beautiful as the JavaScript version, but it does the same thing. Basically the only difference here is, that in JavaScript, you don’t have to use the
array()function to create an associative array, you can just typeand it will work. The PHP alternative here is