TomTees Posted October 29, 2010 Share Posted October 29, 2010 I was given the snippet below and am trying to figure some things out... abstract class FormCollection{ protected $fields = $_POST; protected $validator; protected function __construct($validator){ $this->validator = $validator; } // and so on... } class RegistrationForm extends FormCollection{ private $formInputs = array(); // and so on... } Questions: ----------------- 1.) If I instantiate the concrete class, RegistrationForm, like this... $form = new RegistrationForm(new Validator()); What does that do to the abstract class, FormCollection? Is it getting instantiated too? 2.) I thought you could NOT instantiate an abstract class like FormCollection? 3.) If you can't, then why is there a constructor in the abstract class like FormCollection? 4.) This code seems wrong... protected $fields = $_POST; I was told by somene that PHP doesn't allow you to assign values to a class method at compile time unless the values come from a constant?! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/217242-questiona-about-abstractconcrete-classes/ Share on other sites More sharing options...
darkfreaks Posted October 29, 2010 Share Posted October 29, 2010 4.) from what i have read protected $fields = $_POST // will not work but you can call it like this protected $fields = array(); $this->fields = $_POST; Quote Link to comment https://forums.phpfreaks.com/topic/217242-questiona-about-abstractconcrete-classes/#findComment-1128190 Share on other sites More sharing options...
TomTees Posted October 30, 2010 Author Share Posted October 30, 2010 4.) from what i have read protected $fields = $_POST // will not work but you can call it like this protected $fields = array(); $this->fields = $_POST; I don't believe that will work. Anyone else? What about answer to my other questions as well? Thanks, TomTees Quote Link to comment https://forums.phpfreaks.com/topic/217242-questiona-about-abstractconcrete-classes/#findComment-1128250 Share on other sites More sharing options...
ignace Posted October 30, 2010 Share Posted October 30, 2010 1.) If I instantiate the concrete class, RegistrationForm, like this... $form = new RegistrationForm(new Validator()); What does that do to the abstract class, FormCollection? Is it getting instantiated too? The RegistrationForm extends FormCollection and is therefor an instance of FormCollection this means that RegistrationForm will have all methods of FormCollection inherited. RegistrationForm(new Validator()) is a bad idea because you have no assurance that the data will actually be validated like for example if you by accident pass the wrong validator therefor the validator should be part of the internals of RegistrationForm. 2.) I thought you could NOT instantiate an abstract class like FormCollection? True. You can't instantiate an abstract class or an interface. 3.) If you can't, then why is there a constructor in the abstract class like FormCollection? Because if the concrete class doesn't specify one the parent's constructor get's executed. 4.) This code seems wrong... protected $fields = $_POST; I was told by somene that PHP doesn't allow you to assign values to a class method at compile time unless the values come from a constant?! True. You can only assign scalar types to a class variable, you can also use constant's because they can only hold scalar types. Quote Link to comment https://forums.phpfreaks.com/topic/217242-questiona-about-abstractconcrete-classes/#findComment-1128328 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.