associative array or php object ?

daffy 16 Sep, 2009
use of very simple 'php object' or of an array?

I was setting up an array
$item['id'] = null;
$item['articlenum'] = $articlenum;
$item['cost'] = $cost;

etc etc
so I finish up with an associative array called '$item' which I can use.
But using the 'insert' ability of a database I find from a search that

$thedata->id=null;
$thedata->articlenum=$articlenum;
$thedata->cost = $cost;
seems to create a simple object called 'thedata'

so that
$result = $db->insertObject($thetable,$thedata,'id');
Does the business of inserting

What then is the difference between the array $item and the object $thedata ???
How does $thedata->id as a notation compare with $item['id'] are they interchangable?

Another question

I want to use classes and objects but I find that if I store an instance of a
class in the Joomla session and later recover it I get an error 'incomplete class...'

From my research it seems this is because Joomla starts the session and I later
define the class is there a way round this ??
Can I define classes in some root place in Joomla ?

Any clues would be very welcome
--
Dave
daffy 16 May, 2010
Not so much of a reply as a repeat of the question !!

I am taking 'bookings' and I invent a class 'booking'.
I create an object 'tempbooking' of this class and try to store it in the Session.
When I try to recover it I get an error because apparently classes must be defined before the session is started.
Doing research it seems difficult to know when the session is started ?

Is there a way of killing the session defining the class, then recreating the session ?
1. I am NOT using any 'login'.
2. The point at which I define the class is the very first time AFTER I use chronofrms to make a pseudo login.

Apparently I can hack Joomla code to define my class before Joomla starts the session but I would rather not do that.

If I have to go back to using an array I lose contact with my 'methods'.
Any help appreciated.
--
dave
GreyHead 20 May, 2010
Hi Dave,

The session is created by Joomla when a site page is opened and there is no session. I don't think that there is any way of injecting code beforehand.

I met this problem once before and developed a workaround - there may be better answers!

I stored the object data in the session as an array, created an empty object and repopulated it from the stored session data.

I saved the data with
$session =& JFactory::getSession();
$session->set('booking', $booking->getProperties());

then recovered it with:
$session =& JFactory::getSession();
$booking =& new bookingObject();
if ( $session->has('booking') ) {
  // client is stored as an array to avoid object initialisation errors
  $booking_array = $session->get('booking');
  $booking->setProperties($booking_array);
  $session->clear('booking');
}		}
// the empty object
class bookingObject extends JObject {
    var $booking_id = null;
    var $course_cost = null;
    var $course_id = null;
    var $bike_id = null;
    var $status = null;
    var $payment = null;

    function __construct() {
    }
}

Bob
daffy 21 May, 2010
Thanks bob for taking an interest.
I will study your work-around. I am learning..gradually.
I read that there is a php function 'serialise' and 'unserialise' which can be used to squeeze the object through a session. (I regard it as going into a tunnel at one end and coming out at the other!). Another suggestion was to introduce the class in a 'plugin' triggered by system initialise. So that it is established before any session.
--
dave
This topic is locked and no more replies can be posted.