March 3, 2012

Can I require_once a parent before session_start() to be able to include a child object in $_SESSION?

Question by JDelage

In PHP, I must require_once() the classes of the objects I want to put in session before session_start().

If I want to put in session an instance of a class which is a child of a parent class, can I just require_once() the parent class, or do I need to list all the children classes?

Answer by nategood

If I understand you correctly (assuming that you have individual files with each class), you will need to require_once the file/class for the parent and the child class (otherwise the child class won’t know it’s parent class). session_start should really have nothing to do with it.

Answer by Starx

Yes, you will need to require the child classes & parent classes both. Because

  • It is of child class, of whose you are trying to add an instance
  • The object is the child, will eventually use method from the classes, which might go up to parent classes also

SO, bottom line. Yes, You do need to require all the classes.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!