Jump to content
Larry Ullman's Book Forums

Recommended Posts

I dont understand how the $user object is accessible throughout ch9.

It is created from a pdo call and stored straight into a session. However, at the start of the chapter the footer checks it to see if a user can create page like so:

 

$user->canCreatePage()

 

how is $user available? Should we be using the session to access user?

 

$_seession['user']->canCreatePage()

 

Am i being dense here and missed something dead obvious?  Can an object be stored statically in the same variable and acessible throughout the site   if so why use sessions at all 

Link to comment
Share on other sites

If I understand correctly, one of the first things the pages do is to require the utilities file which autoloads the classes, starts the session and sets the $user variable depending on whether or not $_SESSION['user'] is set.  If it doesnt exist, $user is set to null.

 

When a user logs in the method setFetchMode with PDO::FETCH_CLASS parameter is used to create the $user object and $_SESSION['user'] is created and set to the $user object which PHP automatically serialises so that the object can be stored in the session variable.

 

There is an awful lot of processing going on in a few lines of code because some of the php functions do it all for you. Its good to understand what php is doing so you can use it productively.

 

Good question - made me revisit some of these functions and hopefully my understanding of them has deepened.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...