Jump to content

Recommended Posts

Hi All,

 

My appologies in advance.  I am pretty sure that this has probably been answered before by someone...but for the life of me I carn't find the answer in any forums. 

 

My basic question is this.  Can you create an object at the beginning of a script, and then call its methods from within functions?

 

I have coded the following:

 


<?php

///import classes to use
require "booking_form_gui.php";

$booking = new booking_form_gui();

function _booking_form($form_state){
    
    ///$booking = new booking_form_gui();
   
    return $booking -> date_selection();         <----------this line causes the error I guess


}

 

The problem is that when I run this code, I get an error message similar to the following:

 

Fatal error: Call to a member function date_selection() on a non-object in /var/www/drupal_6/sites/default/modules/booking_system/booking_form/booking_form.module on line 127

 

Anyone any ideas how I could get round this? 

 

Thanks,

 

Mark.

 

Generally using globals is bad form, if you need something inside a function, pass it into that function. 

I agree, but there are a few cases where I'd do it.

Like with a DB object or something. It depends on the script.

Yeah, procedural code with global DB objects generally rely on globals because it's not considered "lazy" there for some reason.  I still dislike it, but I haven't written procedural code in years, so whatever floats your boat. 

 

Either way, OP needs to read that link.

 

-Dan

Hey all,

 

Thanks for the replys.  Sorry its taken me a while to reply. 

 

I hadn't thought about just passing it into the function.  But I am still having issues with this.  I carn't seem to get the execute the objects methods. 

 

For example I tried doing this:

 


///import classes to use
require "booking_form_gui.php";

$booking = new booking_form_gui();

function _booking_form($form_state){
    
    $booking = $GLOBALS['booking'];

    
    $form = $booking -> date_selection();

    return $form;
}

 

However this doesn't seem to work.  I am pretty sure I am missing something here. 

 

I also tried passing it into the function:

 


///import classes to use
require "booking_form_gui.php";

$booking = new booking_form_gui();
function _booking_form($form_state, &$booking){
    
    ///$booking = new booking_form_gui();
    $form = $booking -> date_selection();

    return $form;
}

 

Real appologies for being slow...

 

Thanks in advance for any subsequent posts,

 

Mark.

ok, so I have one script that looks like the following:

 


///import classes to use
require "booking_form_gui.php";

$booking = new booking_form_gui();
function _booking_form($form_state, &$booking){
    
    ///$booking = new booking_form_gui();
    $form = $booking -> date_selection();

    return $form;
}

 

Then the object that I create $booking I hoping to create out of the following code from another class file (the one that I included).  The code for this is:

 


class booking_form_gui {




/**
*  Date Selection Form
* @return string
*/
function date_selection (){

    ///creat $booking as a booking object!
    ///$booking = new booking();

    ///form to allow the the users to select which dates that they want

    ///form input widgets
    $form['start_date'] = array(
        '#type' => 'date_popup',
        '#title' => t('The Start Date'),
        '#date_format' => 'd/m/y',
        '#required' => TRUE,
    );

    ///$form['start_date']['#prefix'] = t("This form will allow to book a "
       ///     ."Non Residential Sailing Holiday"
          ///  ." <br><br>Firstly you need to select a start and end date "
           // ."that you wish to travel on.  After this you will be able to "
           // ."reivew the prices and the places available and alter the dates "
           /// ."if you require.");

    $form['end_date'] = array(
        '#type' => 'date_popup',
        '#title' => t('The End Date'),
        '#date_format' => 'd/m/y',
        '#required' => TRUE,
    );

    $form['add'] = array(
    '#type' => 'submit',
    '#value' => 'Check Prices and Places Available.',
    ////'#submit' => array('add_new_date_form_submit'),
    );

    return $form;
}

 

I should probably point out at this point, that this for a Drupal Module.  I didn't think that it made much difference.  But hopefully it will make some sense?

 

Thanks again for the help

 

Mark.

...It works fine if I create the object in the function.  However I figured that it would be better if I could create the object out side of a function, so that other functions in my script could use the methods and set the attributes...if that makes sense?

 

 

...ok so that get a little specific to drupal I guess...However I can see where you are going with that....

 

Ok the full execution would look like this :

 


<?php
/**
*     <h1>The Actual Booking Form</h1>
*
*This is the actual booking form module that the cleints will use in order to
* book non-residential sailing courses with Minorca Sailing.
*
* Author = Sparky Mark.
*/

///import classes to use
include "booking_form_gui.php";

$booking = new booking_form_gui();

function booking_form_menu(){
///create a $pages array
    $pages = array();

    ///put in the pages

    ///the date selection form
    $pages['the_booking_form'] = array(
        'title' => t('Non Residential Booking Form'),
        'page callback' => 'the_booking_form',
        'access arguments' => array('view non residential booking form'),
        'description' => t('This is the booking form that the clients will use.'),
        'type' => MENU_CALLBACK
    );

}

function the_booking_form(){
    ///seen as its a form we have to call it as one
    return drupal_get_form('_booking_form');
}

function _booking_form($form_state, &$booking){
    
    ///$booking = new booking_form_gui();  
    $form = $booking -> date_selection();

    return $form;
}

 

Ok, so the I can see what you where getting at, I hadn't actually passed the object into the function anywhere!  Ok not sure how this will work to be honest.  Ok I am guessing this is exactly how it works...but I will admit I could be wrong...

 

...So when a request is made to the page 'http://192.168.0.10/drupal_6/?q=the_booking_form' the drupal engine searches for a function called 'the_booking_form'.  For whatever, not exactly sure 100% why but you then have to tell it that its a form its dealing with (drupals quite specific about the way it likes to handle forms...but does have some really cool features).  So it then calls the function 'the_booking_form', which in turn tells drupal that its not a PHP page but a form that its calling...it then calls the function '_booking_form'.

 

Ok with me so far?

 

I can get it work just fine, if I create my booking_form_gui object in the _the_booking_form function.  The problem I have with doing that, once outside of that function I loose all of the functionality of that object - and that something that I would rather keep if I could.

 

Hope that it makes sense what I have asked.  I tried using the <i>globals $booking</i> idea, but I couldn't get it to work at all - I just seemed to get compliation errors.  When I tried using the $GLOBALS['booking'] idea, I hit the same problem as I have now. 

 

The one thing that I have found hard about drupal is that its procedural programming, and really I find it easier to program using Object Orientated - my Java background probably doesn't help matters...

 

...thanks again for all the replies...

 

Mark.

 

 

 

 

If I change the code in the _booking_form module to this:

 


function _booking_form($form_state){

    global $booking;

    return $booking ->date_selection();

}

 

Then I get the following error on execution:

 

Fatal error: Call to a member function date_selection() on a non-object in /var/www/drupal_6/sites/default/modules/booking_system/booking_form/booking_form.module on line 127

 

So I don't see how using the global command could work in this case. 

 

Thanks again,

 

Mark.

...hey that works a treat!

 

Thanks for that.  I remoded the code to look like the following:

 


///import classes to use
include "booking_form_gui.php";

$booking = new booking_form_gui();

$_SESSION['booking'] = $booking;

function booking_form_menu(){

    ///create a $pages array
    $pages = array();

    ///put in the pages

    ///the date selection form
    $pages['the_booking_form'] = array(
        'title' => t('Non Residential Booking Form'),
        'page callback' => 'the_booking_form',
        'access arguments' => array('view non residential booking form'),
        'description' => t('This is the booking form that the clients will use.'),
        'type' => MENU_CALLBACK
    );

}

function the_booking_form(){
    ///seen as its a form we have to call it as one
    return drupal_get_form('_booking_form');
}

function _booking_form($form_state){

    $booking = $_SESSION['booking'];

    $form = $booking ->date_selection();

    return $form;

}

 

And hey presto I get my nice looking drupal form  :D

 

Thanks again for bearing with me.  I really appreciate it.

 

Mark  :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

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