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.