#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Location
    Sydney, Australia
    Posts
    5
    Rep Power
    0
    I am very new to classes and I am probably approaching this from the wrong angle, I want to print the value in class that has a predifined value in the variable, consider this:
    In a file say class.php I have the following code:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    //Create generic class
    class zoneConfig {
    var $colour;
    var $css;
    var $title;
    }

    // Zone configuration

    // setup Games Zone object
    $GAMES = new zoneConfig;
    $GAMES->colour = "bgcolor="#FFFFCC"";
    $GAMES->css = "games.css";
    $GAMES->title = "Games";

    // setup News Zone object
    $NEWS = new zoneConfig;
    $NEWS->colour = "bgcolor="#6699CC"";
    $NEWS->css = "news.css";
    $NEWS->title = "News";
    [/code]

    In a file called zone.php the following:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <?
    if ($zone == 1) {
    $thisZone = new $GAMES;
    }
    else {
    $thisZone = new $NEWS;
    }
    ?>
    <title><? echo ($thisZone->title); ?> </title>
    <body bgcolor="<? echo ($thisZone->colour); ?>"</body>
    etc...
    [/code]
    However I get this: Warning: Undefined property: title in /home/httpd/www/mysite/zone.php on line n
    I can print the value of $GAMES->title though.
    Why isn't $thisZone inheriting the value of title or isn't it meant to?
    As I said I must be missing something or approaching things the wrong way.

    Thanks in advance.
    Graham
  2. #2
  3. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Location
    Sydney, Australia
    Posts
    5
    Rep Power
    0
    Isn't it always the way, post a question and find the answer yourself.
    $thisZone = new $GAMES;
    should be
    $thisZone = $GAMES;

    etc.
    Still think the other should work too, perhaps someone can tell me why not.

    Graham

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Newark, DE, USA
    Posts
    39
    Rep Power
    14
    the other way won't work because $GAMES and $NEWS are aleady objects, as you created them in the class.php file. the NEW operator instantiates an object by using a class definition. in your example class.php file, the use of NEW is correct. you created two objects. then in your zone.php, you're just picking which object you're going to use.

    a suggestion if I may ... instead of creating two objects then picking which you're going to use, it may be cleaner/faster to decide which object to create and use in the zone.php. (creating only one object instead of two)

    ------------------
    ~Chris
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Location
    Sydney, Australia
    Posts
    5
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>a suggestion if I may ... instead of creating two objects then picking which you're going to use, it may be cleaner/faster to decide which object to create and use in the zone.php. (creating only one object instead of two)
    [/quote]

    Thanks for the suggestion, I had considered that, but had not yet had that "moment of clarity" where I was able to work out how to do it.

    If you could spare the time, I'd really appreciate some tips.

    Graham

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Newark, DE, USA
    Posts
    39
    Rep Power
    14
    your class definition (zoneClass.php):
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <?php
    class zoneConfig{
    // ATTRIBUTES
    var $colour;
    var $css;
    var $title;

    // INTERFACES
    // CONSTRUCTOR
    function zoneConfig( $ARG_colour="", $ARG_css="", $ARG_title=""){
    $this->colour = $ARG_colour;
    $this->css = $ARG_css;
    $this->title = $ARG_title;
    }
    }
    ?>
    [/code]
    This lets you pass arguments to the data type when you create an zoneConfig object. Notice that the constructor has the same name as the class. This is necessary if you use a constructor.

    Now you can create an object to use in another script (zoneConfig.php):
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <?php
    include( "zoneClass.php" );
    if( $zone == 1 ){
    $myZone = new zoneConfig( "bgcolor="#FFFFCC"", "games.css", "Games" );
    } else {
    $myZone = new zoneConfig( "bgcolor="#6699CC"", "news.css", "News" );
    }
    ?>

    <title><? echo ($myZone->title); ?> </title>
    <body bgcolor="<? echo ($myZone->colour); ?>"</body>
    etc...
    [/code]
    That just creates one object, and uses the logic to assign attributes to it. There's a basketfull of ways to do this. Give it a whirl any way you feel with which you are comfortable. I hope this helps.

    ------------------
    ~Chris
  10. #6
  11. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Location
    Sydney, Australia
    Posts
    5
    Rep Power
    0
    Thanks Chris, I'll have a play.
    Hope I can return the favour some day.

    Regards
    Graham

Similar Threads

  1. Codepupil
    By Adachic in forum C Programming
    Replies: 3
    Last Post: February 6th, 2004, 06:50 PM
  2. Learning Mouse Event Handling.
    By coredumped in forum Java Help
    Replies: 5
    Last Post: December 4th, 2003, 08:44 AM
  3. class 1 functions used in class 2
    By ioScream in forum PHP Development
    Replies: 4
    Last Post: October 30th, 2003, 03:15 PM
  4. virtual base class inheritance question
    By Rooster in forum C Programming
    Replies: 3
    Last Post: October 18th, 2003, 09:08 AM
  5. compiling .java, thought I knew what I was doing?!
    By geoffgarcia in forum Java Help
    Replies: 10
    Last Post: September 9th, 2003, 10:56 AM

IMN logo majestic logo threadwatch logo seochat tools logo