setter / getter |
[eluser]pouya[/eluser]
How can I set and get my protected/private properties? imagine the code : Code: <?php Is there magic methods to set and get title and body properties from the controller?
[eluser]InsiteFX[/eluser]
Code: public function get_title()
[eluser]pouya[/eluser]
[quote author="InsiteFX" date="1343113861"] Code: public function get_title() thanks but this is the usual way an I knew it! what is CI good for? imagine I have 50 properties then I should write 100 setters and getters!!!
[eluser]Unknown[/eluser]
You could use PHP's magic setter and getter. However, if all you want to do with them is to return and assign values, just make the properties public. The only reason one would want to specify setters and getters is to attach some special logic and/or validation to them, in which case you need to write individual functions to each of them anyway
[eluser]umefarooq[/eluser]
no need to create 100 of setters and getters [url href="http://www.php.net/manual/en/language.oop5.overloading.php"]PHP Property overloading[/url] Code: <?php
[eluser]pouya[/eluser]
thanks guys, it is helpful. I was just wondering whether any setter/getter methods are implemented in CodeIgniter itself just like the Zend framework, and the answer seems to be negative! Unfortunately
[eluser]TWP Marketing[/eluser]
[quote author="pouya" date="1343123149"]thanks guys, it is helpful. I was just wondering whether any setter/getter methods are implemented in CodeIgniter itself just like the Zend framework, and the answer seems to be negative! Unfortunately [/quote] I think a point made here is that CI doesn't carry the bloat for functions that are not used as frequently. If you need it, write a helper and use it only when needed.
[eluser]pouya[/eluser]
[quote author="TWP Marketing" date="1343154272"][quote author="pouya" date="1343123149"]thanks guys, it is helpful. I was just wondering whether any setter/getter methods are implemented in CodeIgniter itself just like the Zend framework, and the answer seems to be negative! Unfortunately [/quote] I think a point made here is that CI doesn't carry the bloat for functions that are not used as frequently. If you need it, write a helper and use it only when needed.[/quote] I don't get it!!! Setting and Getting properties (encapsulation) is an important part in object oriented programming. I have used it in all my projects since I've learned OOP!
[eluser]pouya[/eluser]
[quote author="umefarooq" date="1343121169"]no need to create 100 of setters and getters [url href="http://www.php.net/manual/en/language.oop5.overloading.php"]PHP Property overloading[/url] Code: <?php it was not possible! I found a function named __get() in (./system/core/Model.php) and it makes a conflict with our magic get function! Code: function __get($key)
[eluser]Aken[/eluser]
Lots of options: A) Use your own __get() method, and include logic for whether or not a model property or CI property is being requested. B) Use __call(), and use a method naming structure like get_firstField(), set_firstField(). C) Use a single protected or private array property to hold your setter and getter info, and either use __set() / __get() or define your own methods for retrieving and assigning data. D) Make your properties public and use them directly. |
Welcome Guest, Not a member yet? Register Sign In |