Jump to content
Larry Ullman's Book Forums

titreader

Members
  • Posts

    14
  • Joined

  • Last visited

Posts posted by titreader

  1. According to the book when overriding a parent class must use the same number of parameters. Why is this? What happens when I pass one more parameter. I tested it by passing an extra parameter than the original (parent) signature and it works just fine so I failed to understand the idea behind having the same number of parameters. Can someone clarify this?

     

    Thanks

  2. In Chapter 5 on pate 157 it says in a darker portlet:

     

     

    ...when you create an object of a child class, the parent class's constructor is not automatically called...

     

    I need some more details on this. What concerns me is that I created a file in which I included Rectangle.php then I declared a new class such as:

    class Square extends Rectangle {
    
    }
    

    just an empty class and then called:

    $r = new Square();
    

    for testing purposes I added the following line to Rectangle's constructor class:

    print "construction called";
    

    and it does seem to be executed when new Square class is instantiated with no parameters passed! Is the documentation in the book wrong or am I missing something? Because the above code indicates that the parent constructor seems to be called?

     

    Thanks in advance

  3. I just passed the section of phpdoc about couple of days ago and all worked like a charm. I know this is of any help to you but at least I can confirm that it works as described in the book so you can concentrate on looking for a problem on your side. All I had to do is cd into the directory with the file I want to document and run:

    phpdoc -f HelloWorld.php -t docs
    

    Of course change HelloWorld.php with whatever your file name is and 'docs' with whatever directory you want to generate the documentation in

  4. Hi Antonio,

     

    $data is already serialized as it comes directly from DB. And as per PHP manual it is written there in a serialized form. The error reporting did not bring anything new. Just got an idea how to present you the whole picture. You may download the actual code of the book from here:

     

    http://www.larryullman.com/downloads/phpvqp3_scripts.zip

     

    Unzip it and navigate to Ch03 folder (unfortunately this requires an active DB connection and session table) but you may get around it. So all the functions are in db_session.inc.php What I am trying to achieve is to run sessions.php and print the session_id(). In my case it is always an empty string and I am fighting to understand why.

     

    Thanks for your help so far!

  5. In the tutorial from the book we use this function and a couple of other function like this:

    // Declare the functions to use:
    session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session', 'destroy_session', 'clean_session');
    
    // Make whatever other changes to the session settings, if you want.
    
    // Start the session:
    session_start();
    

    This is actually the end of the db_sessions.inc.php file. So I don't call this function directly but php does when I call $_SESSION

  6. Ah sorry. I've been think all the time that you actually have the book. So this is the function that reads the session from DB located in db_sessions.inc.php [i am providing file names for consistency]:

    // Define the read_session() function:
    // This function takes one argument: the session ID.
    // This function retrieves the session data.
    // This function returns the session data as a string.
    function read_session($sid) {
        global $sdbc;
        session_id($sid);
    
        // Query the database:
        $q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid));
        $r = mysqli_query($sdbc, $q);
    
        // Retrieve the results:
        if (mysqli_num_rows($r) == 1) {
            list($data) = mysqli_fetch_array($r, MYSQLI_NUM);
    
            // Return the data:
            return $data;
    
        } else { // Return an empty string.
            return '';
        }
    } // End of read_session() function.
    

    db_sessions.inc.php starts the session at the end of the file. This file is included in another file called sessions.php Now If i print session_id() in the sessions.php file it returns an empty string. I am failing to understand why!

     

    As you can see php passes the session id hash to this function when reading the session. As per the manual: 

    http://php.net/manual/en/function.session-id.php

     

    one can print the session hash with print session_id() and this is working fine using the default session mechanism of php. When I however change to store the session into DB as per Larry's book session_id() seems to stop functioning. So once you start the session php seems to correctly record it into DB. But if I decide to get the session hash it does not work.

  7. Hi Antonio,

     

    read_session() expects $data to be returned as per the book and the manual so I cannot return session_id(). As per the manual if I am not using database for sessions I can just issue print session_id() and it would print the session hash. But when I switch to database sessions the session_id() function seems to loose its ability and I can't figure out why.

  8. You need to start the session before trying to read the session id. Did you do that?

     

    Everything is as per the book and works. The only thing I've added to the read_session() function is the following line of code:

    session_id($sid);

    This is in the db_sessions.inc.php file. Session is started at the end of the file and the whole file is included into sessions.php where I try to print session_id() but nothing.

×
×
  • Create New...