Object reference not set to an instance of an object error
8 posts
• Page 1 of 1
Object reference not set to an instance of an object error
I am trying to run our web application on Phalanger and I didn't expect it work without some work but I have hit a snag I can't seem to pass. When I create a mysql connection and then try to save it in the class I get the error shown in the subject.
The error happens the instant I try and save the mysql connection into the class property.
Here are the 2 lines that I am using:
public function __construct() {
$conn = mysql_connect($host, $username, $password);
$this->_conn = $conn // Dies here with the error instantly.
}
I am using the mysql extension from your sister project. The connection itself works great - if I just try and use the connection to issue queries it gives back rows as expected. Try and save the connection in the public (or private) property $_conn and things go bad.
Thanks for any help.
The error happens the instant I try and save the mysql connection into the class property.
Here are the 2 lines that I am using:
public function __construct() {
$conn = mysql_connect($host, $username, $password);
$this->_conn = $conn // Dies here with the error instantly.
}
I am using the mysql extension from your sister project. The connection itself works great - if I just try and use the connection to issue queries it gives back rows as expected. Try and save the connection in the public (or private) property $_conn and things go bad.
Thanks for any help.
- compucoder
- Posts: 12
- Joined: August 16th, 2012, 12:29 pm
Re: Object reference not set to an instance of an object err
Hi,
I believe this has nothing to do with MySQL extension.
Do you have a complete test case? Is '$this->_conn' property declared in the class or it is dynamic property?
I believe this has nothing to do with MySQL extension.
Do you have a complete test case? Is '$this->_conn' property declared in the class or it is dynamic property?
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Object reference not set to an instance of an object err
Sorry for such a long gap between replies. I moved on to another big project and I now getting back to using Phalanger.
This issue stil occurs. I have a Class and in the constructor I create the connection which works fine. I then try and save the connection in a private variable. Eg.
I verified that $conn is a valid resource and it can accept queries. I have tried changing the class var to public with no success. It just seems like you can't store a resource in a class variable; this unfortunately is crippling to me as this is my DB abstracyion layer - big job to change how this is done.
This issue stil occurs. I have a Class and in the constructor I create the connection which works fine. I then try and save the connection in a private variable. Eg.
- Code: Select all
Class foo() {
private $_conn = null;
public function _construct($host, $database, $username, $password) {
$conn = @mysql_connect($host, $username, $password);
mysql_select_db($database, $conn);
$this->_conn = $conn; // dies here instantly with object error
}
}
I verified that $conn is a valid resource and it can accept queries. I have tried changing the class var to public with no success. It just seems like you can't store a resource in a class variable; this unfortunately is crippling to me as this is my DB abstracyion layer - big job to change how this is done.
- compucoder
- Posts: 12
- Joined: August 16th, 2012, 12:29 pm
Re: Object reference not set to an instance of an object err
Looks like $this is null ?
Thanks,
Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Object reference not set to an instance of an object err
$this is actually a special varible which means this class or self. You use $this-> to reference properties and methods within the class.
Phalanger handles it just fine for all other cases. So if I had a property like: private $foovar = ''. I can reference that anywhere in the class like: $this->foovar = 'hi'
Here is a more complete example (which works fine in Phalanger)
This works great for everything except when using the mysql_connect() resource - this is where things go bad.
Phalanger handles it just fine for all other cases. So if I had a property like: private $foovar = ''. I can reference that anywhere in the class like: $this->foovar = 'hi'
Here is a more complete example (which works fine in Phalanger)
- Code: Select all
class Foo() {
public $fooVar = '';
public function __construct() {
$this->fooVar = 'hi';
}
}
$f = new Foo();
echo $f->fooVar; // I get Hi
This works great for everything except when using the mysql_connect() resource - this is where things go bad.
- compucoder
- Posts: 12
- Joined: August 16th, 2012, 12:29 pm
Re: Object reference not set to an instance of an object err
I'm really aware of definition of $this
This would be the only cause to get this exception at this line. ('cause $this really can be NULL in PHP ...) The other option would be, the debug information is incorrect, and you're getting the exception one line before.
Do you have whole stack trace of the exception?
Thanks,
This would be the only cause to get this exception at this line. ('cause $this really can be NULL in PHP ...) The other option would be, the debug information is incorrect, and you're getting the exception one line before.
Do you have whole stack trace of the exception?
Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Object reference not set to an instance of an object err
I just recycled the app pool, made a change to the file and now it works. I am unsure what actually fixed this though. The change I made was to comment out mysql_charset() and then I put it back in - then everything works. Very bizarre.
I now got past the opening roadbloack and hit more errors. I will post new threads for the bugs I just found. They are Phalanger and mysql extension bugs I think.
I now got past the opening roadbloack and hit more errors. I will post new threads for the bugs I just found. They are Phalanger and mysql extension bugs I think.
- compucoder
- Posts: 12
- Joined: August 16th, 2012, 12:29 pm
Re: Object reference not set to an instance of an object err
Thank you, I'm looking forward to them
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
8 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 39 guests