Jump to content

when to OOP...


jonniejoejonson

Recommended Posts

If you were creating a social network like facebook...would you create a user class and then when a user logsin, store the user object the they create in a session?..

is this the correct use of classes?

and would you store this object in a session?...

regards to any responders.. j

Link to comment
https://forums.phpfreaks.com/topic/173893-when-to-oop/
Share on other sites

By "store this object in a session" I assume you mean create the object as a session variable -

 

<?php
require 'your_class.php';
session_start();
if(!isset($_SESSION['user_object_name'])){
// create instance
$_SESSION['user_object_name'] = new your_class();
echo 'object created<br />';
} else {
echo 'object exists<br />';
}

$_SESSION['user_object_name']->class_function();  // example call to class function
echo $_SESSION['user_object_name']->class_variable; // example access of class variable
?>

Link to comment
https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916713
Share on other sites

Depending on how I use the class... I may just store a value in the session instead from which I can initiate the class again on the next page and recreate the object.

 

Say my class dealt with file manipulation.. perhaps I just initiate the class with a filename and it gives me all I need from it... so i would just store the filename in the session to call the class again when I need it...

Link to comment
https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916721
Share on other sites

Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition

 

If you use __autoload this is not an issue.  storing an object in a session means the object gets auto-magically serialized and un-serialized - you can also control that using the __sleep and __wake methods within the class itself.

Link to comment
https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-916732
Share on other sites

Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition

 

If you use __autoload this is not an issue.  storing an object in a session means the object gets auto-magically serialized and un-serialized - you can also control that using the __sleep and __wake methods within the class itself.

 

That is if you load it (declare it) before session_start() in both cases. Like I said here:

 

Don't forget that when you use custom objects in sessions you need to load them before session_start on each request otherwise it won't be able to re-create the custom object as it can't find the class definition

Link to comment
https://forums.phpfreaks.com/topic/173893-when-to-oop/#findComment-917098
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.