include file in class

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

    include file in class

    hello!

    Iam wondering how can I include file in class? is it possible?
    is it possible to create object inside another object? for instance if I
    create mysqli connection object inside another class is that doible? or
    one should pass created objekt as parameter to another objekt?

    regarding include it works if I do like this


    PHP Code:
    class myClass{
    function(){
    include 'func.php';
    func(); //returns $var
    //do something with $var
    return $array;
    }

    function1(){
    include 'func.php';
    func();//returns $var
    //do something with $var
    return $array2;
    }
    }



    but it doesn't make eny sence to include it over and over again in every
    function so I would like to that get some thing like


    PHP Code:
    class myClass{
    include 'func.php'; //but this don't work
    func(); // $var=3

    function(){
    //do something with $var
    return $array;
    }

    function(){
    //do something with $var
    return $array2;
    }



    any ideas or comments?

    thanx in advance

  • Colin McKinnon

    #2
    Re: include file in class

    Carramba wrote:
    [color=blue]
    >
    > Iam wondering how can I include file in class? is it possible?
    > is it possible to create object inside another object? for instance if I
    > create mysqli connection object inside another class is that doible? or
    > one should pass created objekt as parameter to another objekt?
    >[/color]

    Yes, but if you'd bothered to try it out you'd have found that although you
    can include within a class/function/condition/loop the included code is
    included in the global scope.

    I suspect it may be possible to achieve the result you want but it's not
    overly clear from your post what that is.

    C.

    Comment

    • Janwillem Borleffs

      #3
      Re: include file in class

      Carramba wrote:[color=blue]
      > but it doesn't make eny sence to include it over and over again in
      > every function so I would like to that get some thing like
      >
      >
      > PHP Code:
      > class myClass{
      > include 'func.php'; //but this don't work
      > func(); // $var=3
      >[/color]

      That's where you can use the constructor for:

      class myClass {
      var $var;

      function myClass() {
      include 'include.php';
      $this->var = getVar();
      }
      }

      However, keep in mind that this is not a very good OO design. If you have a
      collection of utility functions, the best way to use them is through
      static/instantiated method calls or through inheritance:

      Example A (static calls):

      include 'Utils.php';

      class myClass {
      var $var;

      function myClass() {
      $this->var = Utils::getVar() ;
      }
      }


      Example B (inheritance):

      include 'Utils.php';

      class myClass extends Utils {
      var $var;

      function myClass() {
      $this->var = $this->getVar();
      }
      }


      JW




      Comment

      • Carramba

        #4
        Re: include file in class

        thanx!

        It seem to pass an object from a function one should declare

        include 'somefile.php'; //have getObj() with returns object

        class myClass {
        var $var;

        function myClass() {
        $var = getObj();
        }
        }

        then I was trying to use $this->var I ll only get error about unknown
        variable var

        Thanx again

        Comment

        • Janwillem Borleffs

          #5
          Re: include file in class

          Carramba wrote:[color=blue]
          > include 'somefile.php'; //have getObj() with returns object
          >
          > class myClass {
          > var $var;
          >
          > function myClass() {
          > $var = getObj();
          > }
          > }
          >
          > then I was trying to use $this->var I ll only get error about unknown
          > variable var
          >[/color]

          Using: $this->var would be correct. When this triggers errors, the getObj()
          function should be examined.


          JW


          Comment

          Working...