HELP ! ! ! something simple . . . :D

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CorfuVBProgrammer

    HELP ! ! ! something simple . . . :D

    Hello all . . .

    I need a help for something simple.

    I have create two php classes

    for example

    class classname_1
    {

    }

    and the

    class classname_2
    {

    }

    if i like to make an instance of class classname_1 into the classname_2
    how can i do it ? ? ?

    I have try to make one like

    class classname_2
    {
    $obj_class1 = new classname_2();
    }

    but it doesn't work ! ! ! !

    Why ? ? ?

    Thanks a lot . . . :D

  • Kimmo Laine

    #2
    Re: HELP ! ! ! something simple . . . :D

    "CorfuVBProgram mer" <merianosnikos@ gmail.comwrote in message
    news:1167390903 .553328.37000@4 8g2000cwx.googl egroups.com...
    Hello all . . .
    >
    I need a help for something simple.
    >
    I have create two php classes
    >
    for example
    >
    class classname_1
    {
    >
    }
    >
    and the
    >
    class classname_2
    {
    >
    }
    >
    if i like to make an instance of class classname_1 into the classname_2
    how can i do it ? ? ?
    >
    I have try to make one like
    >
    class classname_2
    {
    $obj_class1 = new classname_2();
    }
    >
    but it doesn't work ! ! ! !
    >
    Why ? ? ?
    >
    Thanks a lot . . . :D
    I'd do it like this:

    class A {

    }

    class B {
    private $my_object;
    public function __construct(){
    $this->my_object = new A();
    }
    }

    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • CorfuVBProgrammer

      #3
      Re: HELP ! ! ! something simple . . . :D

      Thanks a lot . . .

      You helped me so much . . . :D

      Comment

      • CorfuVBProgrammer

        #4
        Re: HELP ! ! ! something simple . . . :D

        New Question

        if i like to use the instance from other method into the class how can
        i do that ? ? ?

        example:

        class class_a
        {
        public function method_a()
        {

        }
        }

        class class_b
        {
        private $objInstance;

        public function __construct()
        {
        $this->objInstance = new class_a();
        }

        public function method_t()
        {
        how to use the $objInstance methods into this method ? ? ?
        }
        }



        thanks a lot

        Comment

        • Yoursef

          #5
          Re: HELP ! ! ! something simple . . . :D


          CorfuVBProgramm er a écrit :
          New Question
          >
          if i like to use the instance from other method into the class how can
          i do that ? ? ?
          >
          example:
          >
          class class_a
          {
          public function method_a()
          {
          >
          }
          }
          >
          class class_b
          {
          private $objInstance;
          >
          public function __construct()
          {
          $this->objInstance = new class_a();
          }
          >
          public function method_t()
          {
          how to use the $objInstance methods into this method ? ? ?
          }
          }
          >
          >
          >
          thanks a lot
          You can use objInstance methods like that :
          class class_b
          {
          private $objInstance;

          public function __construct()
          {
          $this->objInstance = new class_a();
          }

          public function method_t()
          {
          $this->objInstance->method();
          }
          }

          Comment

          • CorfuVBProgrammer

            #6
            Re: HELP ! ! ! something simple . . . :D

            This way it works but only into the constructor method.

            I have try to use ot in other methods in the class and doesn't work ! !
            ! ? ? ?

            Comment

            • CorfuVBProgrammer

              #7
              Re: HELP ! ! ! something simple . . . :D

              This way it works but only into the constructor method.

              I have try to use ot in other methods in the class and doesn't work ! !
              ! ? ? ?

              Is any other way ? ? ?

              Comment

              • Michael Fesser

                #8
                Re: HELP ! ! ! something simple . . . :D

                ..oO(CorfuVBPro grammer)
                >This way it works but only into the constructor method.
                Nope.
                >I have try to use ot in other methods in the class and doesn't work ! !
                >! ? ? ?
                Post your code.

                Micha

                Comment

                • kostas1981

                  #9
                  Re: HELP ! ! ! something simple . . . :D


                  CorfuVBProgramm er schrieb:
                  This way it works but only into the constructor method.
                  >
                  I have try to use ot in other methods in the class and doesn't work ! !
                  ! ? ? ?
                  >
                  Is any other way ? ? ?

                  Hello!

                  I'm a PHP newbie, but I've been busy writing much OO-Code over the last
                  years.
                  I had an idea solving this problem using the following scheme:

                  class A
                  {
                  public function methodA(){
                  //some useful code
                  }
                  class B
                  {
                  private A $instanceOfA; //but I am really not sure if this one's works

                  public __construct() {
                  $instanceOfA=ne w A();
                  }
                  public function methodB(){
                  $this->instanceOfA->methodA();
                  }
                  }
                  }
                  Anyway this is legal in terms of OO-Programming and also a well used
                  method for composing classes. I tried this out in PHP but it doesn't
                  seem to work. Obviously you'll have to create the instance within the
                  function/method you want to use it. Like:

                  class B
                  {
                  //....
                  public function methodB(){
                  $instanceOfA=ne w A();
                  $instanceOfA->methodA();
                  }
                  }

                  Well I ran a test with that and it worked. It's not that elegant but I
                  think it makes sense...

                  regards
                  Kostas

                  Comment

                  • Tim Van Wassenhove

                    #10
                    Re: HELP ! ! ! something simple . . . :D

                    class A
                    {
                    public function methodA(){
                    //some useful code
                    }
                    class B
                    {
                    private A $instanceOfA; //but I am really not sure if this one's works
                    >
                    public __construct() {
                    $instanceOfA=ne w A();
                    }
                    public function methodB(){
                    $this->instanceOfA->methodA();
                    }
                    }
                    }
                    Anyway this is legal in terms of OO-Programming and also a well used
                    method for composing classes. I tried this out in PHP but it doesn't
                    seem to work. Obviously you'll have to create the instance within the
                    function/method you want to use it. Like:
                    What you really wanted to do in the constructor of B was the following:

                    public __construct() {
                    $this->instanceOfA = new A();
                    }

                    Because with $instanceOfA you create a local (in that function) variable...


                    --
                    Tim Van Wassenhove <url:http://www.timvw.be/>

                    Comment

                    Working...