Posts

Showing posts with the label function

Call superclass method in object oriented PHP application

You can call parent methods using :: operator, as in example below: parent::init( 'aaa' ); This situation works also, when dealing with static methods.

PHP Function default parameter value

You can set default parameter value for method or function by giving it's initial value at argument declaration, see example: function myFunc( $number = 8 ) { } If client will pass a number to that function like myFunc( 11 ), then $number value will be 11. But if user will call it without argument or parameter, the $number variable will have value of

Organize your global functions to static methods - alternative to structural/procedural programming

If you are not into Object Oriented Programming, and include files and functions everywhere, you can get lost in your own function names. The good alternative is to group your functions into classes with static methods: class UserModule { public static function getUserId() { ... } public static function removeUser( $id ) { ... } } UserModule::removeUser( 11 );

How to access global variable in php ?

If you are in a function on class method scope, you can gain access to a global variable by using a global keyword. $x = 5; function myFunc() { global $x; echo $x; } Remeber that using globals usually isn't a good idea. Better idea would be divide your globals by categories/modules and store it in static variables of some classes.