#1
  1. No Profile Picture
    falcon
    Guest
    Devshed Newbie (0 - 499 posts)
    I'd appreciate any help.

    I have been struggling with this problem for some time and have found nothing on the net of any help.

    I am trying to declare an instance of Class_1 in Class_1. I am able to do it within a function but obviously the function scope only goes as far as the function and disappears once completed.

    If anyone has any suggestions, advice and/or solutions I would be extremely appreciative.

    Falcon
  2. #2
  3. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    Are you talking self-instantiation? You can instantiate inside a method, but you're right it will not carry outside the function UNLESS you make it a property of the class. e.g.:

    class myclass
    {
    var $obj;

    function myfunc($sub)
    {
    $this->obj[$sub]=new otherclass;
    }
    }

    Then if you did:

    $someclass=new myclass;
    $someclass->myfunc("something");

    you could refer to that newly created class as

    $someclass->obj[something]

    and properties/methods as

    $someclass->obj[something]->property

    Note however, that php is not a true OOP language and nesting classes can be unpredictable. I'm not sure what would happen if you used a method to create a new instance of the same class as you describe.

    [This message has been edited by rod k (edited 12-10-99).]
  4. #3
  5. No Profile Picture
    falcon
    Guest
    Devshed Newbie (0 - 499 posts)
    sorry there was a typo in that.

    its supposed to be create an instance of class_1 in class_2.

    class class_2 {

    $foo = new class_1

    function function_name() {
    $this->foo->does_something();
    }

    }

    this is what I am trying to implement but it doesn't seem to work I am unsure why. Sorry I should have seen that typo before submission of the topic.

    Falcon
  6. #4
  7. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    Umm, that's not going to work.

    First, you haven't declared $foo as a class property.

    Second, you have to place $foo=new class_2; inside a method, it won't execute outside a method in a class definition. And you'd need to refer to it as $this->foo=new class_2; otherwise it only would have value inside the method that created. The object would disappear after the method is done executing, unless you pass it via a RETURN.
  8. #5
  9. No Profile Picture
    falcon
    Guest
    Devshed Newbie (0 - 499 posts)
    That is a beaut.

    You have no idea how happy you have made me, thanks very much for your help. I know its a pretty sad thing to get happy over but that has been bugging me for ages.

    The answer seems so simple now. Too much C++ thats my problem.

    Have a fantastic Christmas and an outstanding New Year!!

    Falcon

Similar Threads

  1. Codepupil
    By Adachic in forum C Programming
    Replies: 3
    Last Post: February 6th, 2004, 06:50 PM
  2. Calling a function that is inside a differend class
    By Skull-Splitter in forum C Programming
    Replies: 3
    Last Post: January 31st, 2004, 07:39 AM
  3. PHP data retrieval question with MySQL
    By gursharn in forum PHP Development
    Replies: 7
    Last Post: December 31st, 2003, 09:43 AM
  4. Multiple derived class problems
    By tamer in forum C Programming
    Replies: 2
    Last Post: December 23rd, 2003, 02:28 PM
  5. OO for db connection
    By lazy_yogi in forum Python Programming
    Replies: 22
    Last Post: December 20th, 2003, 09:38 PM

IMN logo majestic logo threadwatch logo seochat tools logo