2

I have a simple example of PHP sessions and AJAX, which works when holding an array in session:

Request file:

<?php
    session_start();
    $_SESSION['data'] = array('foo','bar');
    echo count($_SESSION['data']);
?>
<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
    <button id="but1">Go</button>
    <script type="text/javascript">
        $('#but1').click(function() {
            $.ajax({
                url:'ajaxtest_remote.php',
                success:function(result) {
                    alert(result);
                }
            });
        });
    </script>
</body>
</html>

Remote file:

<?php
    session_start();
    echo 'count=' . count($_SESSION['data']);
?>

The echo on the first file shows 2, and the alert in the success function displays "count=2". Happy days.

Where the problem happens is if I swap my array for a class object:

Request File:

<?php
    session_start();
    include('ajaxtest_class.php');
    $_SESSION['obj'] = new TestClass('foo,bar');
    echo count($_SESSION['obj']->dataList);
?>
<!-- HMTL AS ABOVE -->

Remote File:

<?php
    session_start();
    echo 'count=' . count($_SESSION['obj']->dataList);
?>

Class File:

<?php
    class TestClass {
        var $dataList;
        function TestClass($incoming) {
            $this->dataList = explode(',',$incoming);
        }
    }
?>

This still displays a 2 on the first page, but the ajax success alert comes back "count=0". Can anyone explain why this is?

Update1

If I import the class file into remote it still doesn't work, although I can prove the class is loaded.

<?php
    session_start();
    include('ajaxtest_class.php');
    $c = new TestClass('a,b,c');
    echo 'count=' . count($_SESSION['obj']->dataList) . '-' . count($c->dataList);
?>

The new alert from the ajax success reads count=0-3.

Update2

var_dump($_SESSION['obj']);

object(__PHP_Incomplete_Class)#8 (2) {
    ["__PHP_Incomplete_Class_Name"]=>
    string(9) "TestClass"
    ["dataList"]=>
    array(2) {
        [0]=>
        string(3) "foo"
        [1]=>
        string(3) "bar"
    }
}
2
  • try to debug it both times with a var_dump. are you on an old php4 ? or why do you write your classes like this. php5 way would be to have a function __construct($incoming) Commented Jun 9, 2011 at 14:07
  • I don't get to try oop PHP very often, still very much a novice! So I should just replace the name of my constructor with _construct? Commented Jun 9, 2011 at 14:19

1 Answer 1

6

You would need to include the Class in the remote_ajax file (before session_start()):

edit: The serialize/unserialize requirement is a limitation of PHP4.

Request file:

<?php
include('ajaxtest_class.php');
session_start();
$_SESSION['obj'] = serialize(new TestClass('foo,bar'));

Remote file:

<?php
    session_start();
    include('ajaxtest_class.php');
    $obj = unserialize($_SESSION['obj']);
    echo 'count=' . count($obj->dataList);
?>

In PHP, the class constructor should be defined differently:

<?php
    class TestClass {
        var $dataList;
        function __construct($incoming) {
            $this->dataList = explode(',',$incoming);
        }
    }
?>
Sign up to request clarification or add additional context in comments.

18 Comments

+1 Indeed, unless the class is loaded, it will be a __PHP_Incomplete_Class with no dataList property.
I've made the adjustment exactly as above, but still get count=0
@shanethehat I posted a quick troubleshooting step.. what does var_dump return?
Thanks, I've updated the question with the output. It is an incomplete class, even after importing the class file.
I've figured it out. You must include the class file before calling session_start(). Makes sense I suppose.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.