News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

7PHP.com:
Auto Generate Properties Dynamically For Your Classes Using Magic Methods & Reflection
October 28, 2013 @ 12:57:14

Accessing private class properties via getters and setters is a pretty standard way to write your applications. Unfortunately it can be time consuming to write them for every property your class may have. On 7PHP.com Khayrattee Wasseem has a few ideas (including using PHP's own Reflection functionality) to dynamically create them.

When coding a project, at times (or most of it?) some classes might have more than 3 fields (for whatever reason it suits you). So instead of each time writing and repeating setters and getters (accessor methods), I would like to have a piece of reusable code for all my classes without me ever writing a single line of code for accessors. ('ever' as in 'very very rarely'). Now, we also have to take into consideration that some fields might be only get-able or only set-able - (our re-usable piece of code should cater for this)

He shows two different methods to accomplish this kind of dynamic access, one using traits and the other using normal class inheritance. HE includes the code illustration each solution and talks a bit at the end of each section of why that method might be better than the other.

0 comments voice your opinion now!
reflection getter setter private property tutorial trait inheritance

Link: http://7php.com/magic-dynamic-properties/

blog comments powered by Disqus

Similar Posts

Abhinav Singh's Blog: Setting Nginx, PHP Fastcgi and XCache on a new Ubuntu

Ibuildings techPortal: Installing Magento Enterprise

NetTuts.com: Create Instagram Filters With PHP

Zend Developer Zone: How to avoid Identity Theft in Zend Framework with Zend Auth

Experts Round Table: HTTP Headers and the PHP header() Function


Community Events





Don't see your event here?
Let us know!


conference series tool security voicesoftheelephpant language podcast example laravel interview framework symfony package library list community version composer opinion introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework