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

    Join Date
    Dec 2003
    Posts
    3
    Rep Power
    0

    Creating objects inside other objects.


    Hi all

    Is it possible to create and use objects from inside other objects' functions in php. I'm using version 4.3.3, and either I'm doing something stupid (highly likely) or you can't do it. I have one fairly large class and inside one of its functions I create another object. Now, this new object has a function which takes a bunch of parameters to set up the object (didn't do it in the constructor for other reasons), after which I pass the object to yet another object which has been created in the same function. However, after setting up the first object, all of its values are empty. How can this be? Heres some code to explain it better.

    $result = new CResults();
    $reportGen = new CReportGenerator();

    $result->PopulateResults($strWinners,
    $strLosers,
    $this->arrManagers [$matchNum]["Manager1"],
    $this->arrManagers [$matchNum]["Manager2"],
    $winnersScore,
    $losersScore,
    NULL,
    "",
    "",
    "",
    "",
    "",
    "");
    $reportGen->GenerateReport ($result);

    Remember that all of this happens inside another class. The GenerateReport function is declared like this:

    function GenerateReport (&$results)

    so its not a pass by reference problem. Even if I print any of the variables in the CResults class after calling PopulateResults, they're all empty? Anybody have any insight?

    Andrew
  2. #2
  3. coding with style
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2003
    Location
    Washington
    Posts
    1,254
    Rep Power
    63
    If you want to know if PHP can do something, test it!

    PHP Code:
    <?

    class test2 {
        function 
    print_stuff($stuff) {
            echo 
    $stuff;
        }
    }

    class 
    test {
        function 
    create_class() {
            
    $test2 = new test2;
            
    $test2->print_stuff("this is some stuff");
        }
    }


    $test = new test;
    $test->create_class();

    ?>
    - Nycto

IMN logo majestic logo threadwatch logo seochat tools logo