Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi, still teaching myself, Larry's books are great. As I am not in the computer field, when I go to places like PHP.net for info I usually can't figure out what they are really trying to say to me. I hope that my question below is proper for this forum, if not please excuse my question and I will respectfully withdraw it.

 

I bought the PHP 5 Advanced book really to learn OOP, something very new to me. I am able to work through Larry's examples an do eventually get things to work when I modify the examples, to be sure I understand how the coding works.

 

I took Script 3.1 db_sessions.inc.php and decided to turn it into an object, originally I wasn't able to get it to work no matter what I tried. On PHP.net I saw:

 

"When using objects as session save handlers, it is important to register the shutdown function with PHP to avoid unexpected side-effects from the way PHP internally destroys objects on shutdown and may prevent the
write
and
close
from being called. Typically you should register
'session_write_close'
using the
function."

 

I tried to figure out what this meant but wasn't able to make my script work, until I found an example on the Internet in a chat room that initiated the the function like this below, not by just sending the objects, but by passing each of them in a different array along with a '$this' as a separate array item:

 

session_set_save_handler( array($this,'OpenSession'),
    array($this,'CloseSession'),
    array($this,'ReadSession'),
    array($this,'WriteSession'),
    array($this,'DestroySession'),
    array($this,'GarbageCollectionSession'));

 

 

Then everything worked, "CloseSession, ReadSession, WriteSession, DestroySession and GarbageCollectionSession are all objects that do their respective tasks.

 

Can someone tell me why passing them as arrays with item [0] = "$this", and then item [1] = "TheObject" works, and also what kind of parameter have I passes. I would have not guessed that I could pass an array to a function in that manner without first assigning it to a variable?

 

I am using XAMPP, PHP 5.3.8 and am on a Windows 7 PC.

 

Any insight would be appreciated.

 

Tony

Link to post
Share on other sites

These functions are procedural, in other words, not object oriented. Because you need them to work with your objects, you need those functions to have an object to work with. The thing you are describing here is not a common programming thing. It's a PHP thing because of how some of these functions are designed.

 

I recently debugged a very similar problem for a friend. I could not understand why his anonymous function was not able to work with data inside his objects. I found a similar solution as your did, but believe me, that was in no way obvious. I have experience in other fully OOP languages, but I've never had to do things like that.

 

The thing here is that session_set_save_handler() is really a procedural function. Look at other ways to handle objects in sessions. Session handling in objects are kind of a pain, but you can have at it here: http://www.php.net/manual/en/language.oop5.magic.php#object.sleep

  • Upvote 1
Link to post
Share on other sites

Thank you Antonio, it's nice to hear that I wasn't just missing something basic about OOP. Any idea what is actually going on when those array type parameters are passes? Any reason why it makes the procedural function work with objects?

Link to post
Share on other sites

You can now create a class that implements the SessionHandlerInterface in the Standard PHP Library, then create an instance of that class and pass it to set_session_save_handler(). There's an example in the PHP manual.

  • Upvote 1
Link to post
Share on other sites

Thank you Larry, like I said, I am new to this stuff and the PHP manual is written for someone who knows this stuff and explains how to implement it in PHP, so for the most part I have trouble digesting it at best, and at worst I never really understand what it is saying. That's why I appreciate your books so much, you explain why and how things work with practical examples.

 

By the way, any idea how/w the example code I started the topic works in calling the procedural function using object methods?

 

Thanks,

 

Tony

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...