Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Hi all, having recently taken the plunge, I am pretty new to OO php...

    I am trying to see an object ($caravan_list) from inside a class here is my example code:

    class caravan_search {

    function __construct() {
    global $caravan_list;
    $source = "http://www.somewhere.xml";
    $caravan_list = new SimpleXMLElement($source,null,true);

    }

    function display($node_no) {
    global $caravan_list;
    $output .= $caravan_list->caravan[$node_no]->year_made;
    return $output;

    }

    }

    ------------------------

    when I do:

    $search = new caravan_search();

    echo "year made obj:".$search->caravan_list->caravan[5]->year_made."<br><br><br>";

    echo "year made function:".$search->display(5);

    ------------------------------
    I get:

    year made obj:

    year made function:2002

    IE nothing from the object but the menthod works...

    Any comments would be great thanks -Hagen
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  2.  

  3. #2
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    It is quite bad practice to use the global keyword, and it's not part of OO, it's used in procedural programming.

    And you can't access caravan_list from a global keyword in the construct, what you have to do is make it a class variable - and this is where you're having the problems.

    What you need is something more like:
    Code:
    class Caravan_Search {
    
        public $caravanList;
    
        public function __construct() {
            $source = "http://www.somewhere.xml";
            $this->caravanList = new SimpleXMLElement($source,null,true);
        }
    
        public function display($node_no) {
            $output = $this->caravanList->caravan[$node_no]->year_made;
            return (string)$output;
        }
    
    }
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  4. #3
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Hiya, thanks for the reply... yes I did try that.... then included is as follows:

    Code:
    include "jxwd-env.php";
    
    $search = new caravan_search();
    
    echo "year made obj:".$search->caravan_list->caravan[5]->year_made."<br><br><br>";
    
    echo "year made function:".$search->display(5);
    just outputs noting IE :

    year made obj:

    year made function:

    Any ideas??
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  5. #4
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    ahhhh getting there had some dogey syntax... think I have it now...

    Thanks -H
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:28 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com