473,788 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoke function from initiated class within a class

66 New Member
[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?
Aug 24 '07 #1
11 2158
Atli
5,058 Recognized Expert Expert
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 New Member
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.
Aug 24 '07 #3
Atli
5,058 Recognized Expert Expert
[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.
Aug 24 '07 #4
eros
66 New Member
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 New Member
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 New Member
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 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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

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

Similar topics

3
14952
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
1603
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
13214
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 stack and continue execution at the point of the initial call. Is that possible? Thanks, Bill
0
2961
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 thread. so far so good. all contacts r added properly. now when another login adds me in his contact, i recv a subscription, so i popup a form and ask for accept/reject. this all happens in a separate thread. popup form gets opened and choice is...
1
1996
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
1970
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 String) txtChat.AppendText(msg) End Sub I've never used a delegate or invoke before and I'm rather confused on the topic. Hope someone can help.
11
11263
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 the onclick attribute of the second button to the javascript to close the form after user clicks upload button click upload do the update to database then programmatically click the second button the use on onclick attribute that sees the...
6
39439
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, string s) 2. MyDelegate MyDelegateVar = MyMethod 3. MyForm.Invoke (MyDelegateVar) Some questions:
3
3241
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 code is: Public Class MyClass1 Implements System.ComponentModel.ISynchronizeInvoke Private _InvokeRequired As Boolean = False Public Function BeginInvoke(ByVal method As System.Delegate, ByVal args() As Object) As...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.