Hi there, I am pretty new to writing classes (and pretty new to PHP itself), but I was wondering what was the best format for constructing
There are a few general patterns that show up time after time. They happen so often that there are formal names for them. This isn't really a PHP question per se, but a good site for exploring object design in PHP is http://phppatterns.com/
classes. Now for example, i have written 2 versions of a class that accomplish the exact same thing. And I was just wondering if there are any advantages to either, or even if one was formatted more so to standards. Just wanting to learn it the proper way from the get go. My classes are formatted for use in flash remoting (hence the methodTable stuff - you can ignore it). I guess my question refers more towards the declaration of properties, etc.
Think of declaration of properties and methods as a "contract". When something is public it is available to all of PHP. When it is private it is only usable by the class that you define it in. When it is protected it is a hybrid; it is usable to the class that defined it and it can be "inherited" by classes that extend that class.
So, decide what level of access you really *need* for that property / method. If a property is only supposed to be modified by class methods (for example, a password string) then make it private or possibly protected. If everything is public access then there is temptation to do something like:
<?php
class myObject {
public $pubprop = 'I am the starting value. Trust me, even though I am public access';
}
function all_hell_breaks_loose($obj) { $obj->pubprop = 'ANARCHY LIVES! PHEAR DA WRAFF OF DA PUBIC PROPS!'; }
$obj = new myObject();
/** ... 1000's of lines of code ... */
all_hell_breaks_loose($obj); print_r($obj);
?>
-- Teach a man to fish...
NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://www.php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php
LAZY | http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php