Posts

Showing posts with the label class

Inheritance in PHP

Inheritance (extending classes) is realised in PHP with extends keyword: class albumController extends MfStandardController { Now, object of class albumController will be of type albumController, but also of type MfStandardController, because MfStandardController is a parent of this class

How to access property of an object in PHP ?

You can access property of an object in php by opeartor ->.Just place it after a object variable, but remeber that accessed property must have valid access like private/protected/public.You can't access a private or protected object property from outside of a class or subclass.Item that you are accessing have to be public. You can access protected properties from subclasses and private properties if you are working with class wich have that property.Here is example how to access a public property in a object: class Sample { public $value; } $object = new Sample(); $object->value = 8; Remeber that good practice says to use getter and setter methods. If your project at some point will needa refactor that accessing some variable needs additional action then you can just change get or set method for that varaible.

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.