Jump to content

Recommended Posts

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

 

 

 

 

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

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.