Invoke function from initiated class within a class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eros
    New Member
    • Jun 2007
    • 66

    Invoke function from initiated class within a class

    [PHP]
    <?php
    //clsrecordset.ph p
    class Recordset extends Database{

    public function Recordset( ) { }

    public function query( ) { }

    public function endOfFile ( ) { }

    }
    ?>[/PHP]

    [PHP]<?php
    //clssearchindex. php
    require_once('c lsrecordset.php ');

    class Searchindex {
    private c_rs;

    public function Searchindex ( ) {
    $this->c_rs = new Recordset( );
    }

    public function getSQL ( ) { }

    public function getData ( ) {
    $rs = $this->c_rs->query($this->getSQL()); // error occurred this line
    $this->c_rs->query($this->getSQL());
    if ( $this->c_rs->endOfFile() ) {
    $norecs = true;
    }
    }
    }
    ?>[/PHP]

    Error message: Call to a member function query() on a non-object

    Additional, when I type the $this->c_rs-> , there is no intellisense on this case that's why I know there is something wrong and couldn't find the function.

    Help: How can I call the function from the initiated class in order to use in other class?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Are you sure you have called the method that initializes the c_rs object?

    You should consider adding a constructor to initialize it, then there will be no risk of the variable not being initialized.

    Comment

    • eros
      New Member
      • Jun 2007
      • 66

      #3
      Originally posted by Atli
      Are you sure you have called the method that initializes the c_rs object?

      You should consider adding a constructor to initialize it, then there will be no risk of the variable not being initialized.
      [PHP]<?php
      $pg = new Searchindex ( );
      $pg->getData ( );
      ?>[/PHP]

      Yes Sir, I use the new operator during initiation of the class then it is automatically invoke the Searchindex con structor.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by eros
        [PHP]<?php
        $pg = new Searchindex ( );
        $pg->getData ( );
        ?>[/PHP]

        Yes Sir, I use the new operator during initiation of the class then it is automatically invoke the Searchindex con structor.
        Yea sorry, totally overlooked that. I've gotten to used to the __construct() syntax :)

        The error is where you create the c_rs variable, you forgot the $ before the variable name.

        Comment

        • eros
          New Member
          • Jun 2007
          • 66

          #5
          Originally posted by Atli
          Yea sorry, totally overlooked that. I've gotten to used to the __construct() syntax :)

          The error is where you create the c_rs variable, you forgot the $ before the variable name.
          I think it is documented in php documentation when accessing the members itself, you only need the member name before $this-> .

          For example:
          [PHP]<?php
          class MyClass {
          private var1;
          public var2;

          public function MyClass ( ) {
          // variable members initialization
          $this->var1 = 1;
          $this->var2 = "variable2" ;
          // also calling function members
          $this->MyFunction ( );

          // "$" should be use when you need local variable.
          // Therefore, $this->var? and $var? are treated differently.
          $var1 = 2;
          $var2 = "variable2" ;

          $result = $this->var1 + $var1; // output 3
          }

          public function MyFunction ( ) { }
          }
          ?>[/PHP]

          Comment

          • eros
            New Member
            • Jun 2007
            • 66

            #6
            The above are the basics of PHP OOP, therefore it is not questionable unless I overlooked the code after my several times of investigations of what is really happening.

            I created this topic because I feel strange of the error.. It must be correct syntactically and logically....

            Comment

            • eros
              New Member
              • Jun 2007
              • 66

              #7
              Invoke function from initiated class within a class [CLOSED]

              I am very sorry guys I just missed looked / overlooked the code.

              The code posted here was different in my source code.

              [PHP]<?php
              class Searchindex ( ) {
              private $c_rs;

              public function Searchindex ( ) {
              /* this is the problem.
              * It should be $this->c_rs = new Recordset ( );
              */
              $c_rs = new Recordset ( );
              }
              }
              ?>[/PHP]

              I make my own headache ;).. Thanks guys.

              TOPIC CLOSED

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Glad your code is working now.

                Just to clear things up tho, take a look at this:
                [code=php]
                class myclass
                {
                // This is incorrect, and will cause errors!
                private myVar;

                // This is correct
                private $myvar;

                function testFunc() {
                // This is correct, and will always refer to the variable
                // we declared before the function.
                echo $this->myvar;

                # EDIT, got this wrong the first time. It has been corrected.
                // This is not valid, it appears that the class variable
                // $myvar does not exist inside the scope of this function.
                // This will cause PHP to print a warning, if warnings are enabled.
                echo $myvar;
                }
                }
                [/code]
                Last edited by Atli; Aug 24 '07, 03:09 PM. Reason: Fixed a mistake in the code.

                Comment

                • eros
                  New Member
                  • Jun 2007
                  • 66

                  #9
                  Originally posted by Atli
                  Glad your code is working now.

                  Just to clear things up tho, take a look at this:
                  [code=php]
                  class myclass
                  {
                  // This is incorrect, and will cause errors!
                  private myVar;

                  // This is correct
                  private $myvar;

                  function testFunc() {
                  // This is correct, and will always refer to the variable
                  // we declared before the function.
                  echo $this->myvar;

                  // This is also correct, because the myvar variable
                  // is declared inside the class, but any instances of
                  // of $myvar inside the function will take over the scope.
                  echo $myvar;
                  }
                  }
                  [/code]
                  Do you mean $this->myvar and $myvar are the same memory location? It means changes in $myvar are automatically reflected in $this->myvar and viceversa?

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Originally posted by eros
                    Do you mean $this->myvar and $myvar are the same memory location? It means changes in $myvar are automatically reflected in $this->myvar and viceversa?
                    No, it doesn't appear to work like that. I thought it did tho :/
                    Which means my earlier post was wrong. (I've edited it btw to show the error)

                    I made a simple test:
                    [code=php]
                    class myclass
                    {
                    private $myvar;

                    function myfunc()
                    {
                    $this->myvar = 5;

                    echo $myvar; // = 'Undefined wariable' warning.
                    echo $this->myvar; // = 5

                    $myvar = 10;

                    echo $myvar; // = 10
                    echo $this->myvar; // = 5
                    }
                    }
                    [/code]

                    So based on that, class variables can not be used inside class methods without $this.

                    It is possible, however, that this behavior may vary based on your configuration. This test was done using the default configuration.

                    Comment

                    • eros
                      New Member
                      • Jun 2007
                      • 66

                      #11
                      Yes that's it, Thank you very much.. It is highly appreciated.

                      TOPIC CLOSED

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Heya, Eros.

                        Class variables are DECLARED using '$' notation, but they are ACCESSED using '$this->'.

                        When you do this:
                        [code=php]
                        protected $myVar;
                        [/code]

                        You are telling PHP to allocate memory for a protected variable named myVar.

                        The problem starts to manifest when you do something like this (which you should never do, but it is technically possible):
                        [code=php]
                        public function doSomething($my Var)
                        {
                        echo $myVar;
                        }
                        [/code]

                        What are we echoing here? The $myVar parameter to doSomething()? Or the protected member $myVar?

                        Hence, the $this notation:
                        [code=php]
                        public function doSomething($my Var)
                        {
                        echo $this->myVar;
                        }
                        [/code]

                        Since $this->myVar is the full reference to the variable, you only need to use one '$' sign.

                        Note that this would probably give you an error:
                        [code=php]
                        echo $this->$myVar;
                        [/code]
                        because PHP would evaluate $myVar, and then attempt to echo $this->{value of $myVar}.

                        For more info, check out this document.

                        Comment

                        Working...