PHP Coding Tips

Monday, August 20, 2012

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.

No comments:

Post a Comment