473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Invoke function from initiated class within a class

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

public function Recordset( ) { }

public function query( ) { }

public function endOfFile ( ) { }

}
?>[/PHP]

[PHP]<?php
//clssearchindex.php
require_once('clsrecordset.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?
Aug 24 '07 #1
11 2125
Atli
5,058 Expert 4TB
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.
Aug 24 '07 #2
eros
66
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 constructor.
Aug 24 '07 #3
Atli
5,058 Expert 4TB
[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 constructor.
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.
Aug 24 '07 #4
eros
66
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]
Aug 24 '07 #5
eros
66
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....
Aug 24 '07 #6
eros
66
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
Aug 24 '07 #7
Atli
5,058 Expert 4TB
Glad your code is working now.

Just to clear things up tho, take a look at this:
Expand|Select|Wrap|Line Numbers
  1. class myclass
  2. {
  3.   // This is incorrect, and will cause errors!
  4.   private myVar;
  5.  
  6.   // This is correct
  7.   private $myvar;
  8.  
  9.   function testFunc() {
  10.     // This is correct, and will always refer to the variable
  11.     // we declared before the function.
  12.     echo $this->myvar;
  13.  
  14.     # EDIT, got this wrong the first time. It has been corrected.
  15.     // This is not valid, it appears that the class variable
  16.     // $myvar does not exist inside the scope of this function.
  17.     // This will cause PHP to print a warning, if warnings are enabled.
  18.     echo $myvar;
  19.   }
  20. }
  21.  
Aug 24 '07 #8
eros
66
Glad your code is working now.

Just to clear things up tho, take a look at this:
Expand|Select|Wrap|Line Numbers
  1. class myclass
  2. {
  3.   // This is incorrect, and will cause errors!
  4.   private myVar;
  5.  
  6.   // This is correct
  7.   private $myvar;
  8.  
  9.   function testFunc() {
  10.     // This is correct, and will always refer to the variable
  11.     // we declared before the function.
  12.     echo $this->myvar;
  13.  
  14.     // This is also correct, because the myvar variable
  15.     // is declared inside the class, but any instances of
  16.     // of $myvar inside the function will take over the scope.
  17.     echo $myvar;
  18.   }
  19. }
  20.  
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?
Aug 24 '07 #9
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. class myclass
  2. {
  3.     private $myvar;
  4.  
  5.     function myfunc()
  6.     {
  7.         $this->myvar = 5;
  8.  
  9.         echo $myvar; // = 'Undefined wariable' warning.
  10.         echo $this->myvar; // = 5
  11.  
  12.         $myvar = 10;
  13.  
  14.         echo $myvar; // = 10
  15.         echo $this->myvar; // = 5
  16.     }    
  17. }
  18.  
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.
Aug 24 '07 #10
eros
66
Yes that's it, Thank you very much.. It is highly appreciated.

TOPIC CLOSED
Aug 25 '07 #11
pbmods
5,821 Expert 4TB
Heya, Eros.

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

When you do this:
Expand|Select|Wrap|Line Numbers
  1. protected $myVar;
  2.  
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):
Expand|Select|Wrap|Line Numbers
  1. public function doSomething($myVar)
  2. {
  3.     echo $myVar;
  4. }
  5.  
What are we echoing here? The $myVar parameter to doSomething()? Or the protected member $myVar?

Hence, the $this notation:
Expand|Select|Wrap|Line Numbers
  1. public function doSomething($myVar)
  2. {
  3.     echo $this->myVar;
  4. }
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. echo $this->$myVar;
  2.  
because PHP would evaluate $myVar, and then attempt to echo $this->{value of $myVar}.

For more info, check out this document.
Aug 26 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: Harshankumar | last post by:
Hello, I have the following questions 1) can we pass the parameters to interrupt? 2)can we return the parameters from interrupt Regards harshan
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
1
by: Lore Leunoeg | last post by:
If I call a delegates BeginInvoke a new thread from the threadpool is started. But what happens if I call the Invoke method? Does this also start a new thread? Thank you sincerely Lore
9
by: Terry Olsen | last post by:
I'm running an asynchronous Socket. In the ReceiveCallback method, I need to append what is received to a textbox on the main form. I have this code: Private Sub ToChatWindow(ByVal msg As...
11
by: cindy | last post by:
I have a form, has javascript registered so a modal pops up. Button click will close form. Now I need to do an update with modal form data before it closes. I can put a second button and register...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.