May 1, 201214 yr I'm trying to write a PHP class that encapsulates public / private / protected / public / abstract / final / static / const behavior in PHP. I believe I got this much right: - public, protected and private are mutually exclusive and can be used for class methods and properties but not for classes - abstract and final can be used for classes and class methods - abstract methods only have signatures but I'm not clear about these: - abstract and final are mutually exclusive? (if not, what is the purpose of an abstract final method?) - const class members are always public? (that would mean that const and protected/private are mutually exclusive) - static can be use for class members and can be combined with public/private/protected? - final can be used for class properties? (why?) I'd be grateful if anyone could clarify any of this. So far I have this code: <?php class Attributes { public function get_abstract() { return $this->abstract; } public function set_abstract($abstract) { if(is_bool($abstract)) { $this->abstract = $abstract; } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } public function get_private() { return $this->private; } public function set_private($private) { if(is_bool($private)) { $this->private = $private; $this->protected = !$private; $this->public = !$private; } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } public function get_protected() { return $this->protected; } public function set_protected($protected) { if(is_bool($protected)) { $this->protected = $protected; $this->private = !$protected; $this->public = !$protected; } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } public function get_public() { return $this->public; } public function set_public($public) { if(is_bool($public)) { $this->public = $public; $this->private = !$public; $this->protected = !$public; } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } public function get_static() { return $this->static; } public function set_static($static) { if(is_bool($static)) { $this->static = $static; } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } private $abstract = false; private $private = false; private $protected = false; private $public = true; private $static = false; private $final = false; private $const = false; } ?> Thanks in advance.
May 2, 201214 yr - abstract and final are mutually exclusive? (if not, what is the purpose of an abstract final method?) I've been coding PHP for years and never used a final method in an abstract class. I guess you might want to use it to stop overloading methods from the abstract parent. e.g if you had a strict constructor that the some code later relied on the class being instantiated in a certain way. (which probably constitutes bad application design) - const class members are always public? (that would mean that const and protected/private are mutually exclusive) This is a design trait of PHP, because a constant cannot be redefined there would be no reason to restrict via a protected or private identifier. In PHP access identifiers are there to restrict changing values, not accessing. Accessing members without a getter is dirty. - static can be use for class members and can be combined with public/private/protected? Yes, if it was a protected or private static member you would access it using self::myVar or parent::myVar - final can be used for class properties? (why?) To stop overwriting. Again, I can count the number of times I've used a final class on my hand, and most of them have been for crude debugging. Typically you would use it on utility classes which are fundamental in your application. e.g final class Integer { public function get() { $random = rand(); return (integer) $random; } } // would stop something like this happening class TrollInteger extends Integer { public function get() { $random = rand(); return (string) $random; } } I don't know what you're trying to achieve with that object you posted. If you want to try all that stuff I would split it up into practical examples. Do you need any more help? I can post some examples if you need anything.
May 3, 201214 yr Think the only time final would be handy is for Library writers, so you can make sure some functions dont get overwritten by child classes and other developers and not use private.... but using absract and final would be extreamly odd as Jock says, Also google did this guide years ago, which basicly has 3 valid points Avoid writing naive setters and getters Don't copy variables for no reason Avoid doing SQL queries within a loop https://developers.google.com/speed/articles/optimizing-php "- final can be used for class properties? (why?)" Note: Properties cannot be declared final, only classes and methods may be declared as final. also checkout this section of the php manual: http://uk3.php.net/manual/en/language.oop5.php
May 3, 201214 yr Author Thanks everybody for answering. I am trying to write a PHP code generator so I need a class to account for all possible combinations for classes, methods and properties. I realized I was trying to do too much in one class so I split the logic like this: abstract can be applied to classes, methods and properties so I have this as a base class: <?php class Attributes { public function get_abstract() { return $this->abstract; } public function set_abstract($abstract) { if(is_bool($abstract)) { $this->abstract = $abstract; } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } protected $abstract = false; } ?> Classes can be abstract or final but not both, so I have this class for class attributes: <?php include_once 'Attributes.php'; class ClassAttributes extends Attributes { public function set_abstract($abstract) { parent::set_abstract($abstract); if($abstract) { $this->final = false; } } public function get_final() { return $this->final; } public function set_final($final) { if(is_bool($final)) { $this->final = $final; if($final) { $this->abstract = false; } } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } private $final = false; } ?> private, protected, public and static can be applied to both methods and fields. private, protected and public are mutually exclusive, but abstract and static can be used in combination with any of them: <?php include_once 'Attributes.php'; class ClassMemberAttributes extends Attributes { public function get_private() { return $this->private; } public function set_private($private) { if(is_bool($private)) { $this->private = $private; if($private) { $this->protected = false; $this->public = false; } } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } public function get_protected() { return $this->protected; } public function set_protected($protected) { if(is_bool($protected)) { $this->protected = $protected; if($protected) { $this->private = false; $this->public = false; } } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } public function get_public() { return $this->public; } public function set_public($public) { if(is_bool($public)) { $this->public = $public; if($public) { $this->private = false; $this->protected = false; } } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } public function get_static() { return $this->static; } public function set_static($static) { if(is_bool($static)) { $this->static = $static; } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } protected $private = false; protected $protected = false; protected $public = true; protected $static = false; } ?> const can be used for fields, but not methods: <?php include_once 'ClassMemberAttributes.php'; class FieldAttributes extends ClassMemberAttributes { public function get_const() { return $this->const; } public function set_const($const) { if(is_bool($const)) { $this->const = $const; if($const) { $this->abstract = false; $this->private = false; $this->protected = false; $this->public = false; } else { $this->public = true; } } else { throw new InvalidArgumentException( 'The value is not boolean.'); } } private $const = false; } ?> (I still need another child class to account for final methods.) That's my current code, hopefully I'm getting close
Create an account or sign in to comment