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

    Join Date
    Oct 2003
    Posts
    61
    Rep Power
    15

    class 1 functions used in class 2


    I haven't tried this yet, though, I'm getting ready to write a class for generating forms, because I've yet to find one that is straight forward form generation, no validation. The ones I've found are missing a lot of options that I need, and I hate dealing with other people's code, or lack there of. I just want to make sure that I can call a function outside of one class to another class.

    Example:
    class1 contains
    function poop()

    class 2 contains and uses function poop() from class 1
    function flush()
    poop(wipe);

    I'm curious how I would go about it. I assume I would initiate class 1 within the function in class 2.
    $new = new class1;

    Like I said, I haven't tried this yet. Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    61
    Rep Power
    15
    Well, seeing how no one else would answer my post, I went ahead and tested it. In case anyone is curious, you can use functions from one class to another by creating a new instance.
  4. #3
  5. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,196
    Rep Power
    2268
    Its like this:
    PHP Code:

    class bm
    {
         function 
    poop()
         {
              return 
    "plop";
         }
    }

    class 
    toilet
    {
         function 
    flush()
         {
              
    $what_to_flush bm::poop();
              unset(
    $what_to_flush);
              return 
    TRUE;
         }
    }

    $toilet = new toilet;
    $didit = ($toilet->flush()) ? "Yep, its done" "Something went wrong! Eat more prunes!";
    echo 
    $didit
    Read about the :: operator here:
    http://us2.php.net/manual/en/language.oop.php
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
    The Most Important Article You Can Read as a Developer
  6. #4
  7. No Profile Picture
    <? unset($sanity) ?>
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Posts
    613
    Rep Power
    15
    Originally posted by drgroove
    Its like this:
    PHP Code:

    class bm
    {
         function 
    poop()
         {
              return 
    "plop";
         }
    }

    class 
    toilet
    {
         function 
    flush()
         {
              
    $what_to_flush bm::poop();
              unset(
    $what_to_flush);
              return 
    TRUE;
         }
    }

    $toilet = new toilet;
    $didit = ($toilet->flush()) ? "Yep, its done" "Something went wrong! Eat more prunes!";
    echo 
    $didit
    Read about the :: operator here:
    http://us2.php.net/manual/en/language.oop.php
    I'm sorry, but the example name in this post made me laugh a little.. Great way to use an example.
    "I haven't failed, I've found 10,000 ways that won't work."
    - Thomas Edison

    -=Rick=-

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

    Join Date
    Sep 2003
    Posts
    99
    Rep Power
    15
    PHP Code:
    class bm
    {
         function 
    poop()
         {
              return 
    "plop";
         }
    }

    class 
    toilet extends bm
    {
         function 
    flush()
         {
              
    $what_to_flush $this->poop();
              unset(
    $what_to_flush);
              return 
    TRUE;
         }
    }

    $toilet = new toilet;
    $didit = ($toilet->flush()) ? "Yep, its done" "Something went wrong! Eat more prunes!";
    echo 
    $didit
    will also work

IMN logo majestic logo threadwatch logo seochat tools logo