You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4php-user@logging.apache.org by Michael Sole <ms...@gold-mobile.com> on 2011/08/31 18:46:34 UTC

Can not access object from extended class

I have an application that uses a base Database class and then each
class extends that. This gives easy database access to all the classes
and insures that proper logging is maintain by all my developers.

 

If I create a new instance of the database class directly the logging
works fine, when I create a new instance of the class that extends my
database class I get an error when access any of the log4php log
methods.

 

I can add the getLogger method to each of the parent classes but this is
less than ideal. Is the a way to make log4php methods available given
the use cases I just outlined?

 

Thanks

 

_______________

Michael F. Sole
msole@gold-mobile.com

Office: 732.632.8801, ext. 261
Mobile: 516.592.3683

Fax: 732.632.3599

Gold Mobile
www.gold-mobile.com <http://www.gold-mobile.com/>   

Mobile Health Tech Group
www.mobilehealthtech.md <http://www.mobilehealthtech.md/> 

This e-mail and any files transmitted are intended solely for the use of
the individual or entity to whom they are addressed, and for the use of
Gold Mobile.  Any other use is strictly prohibited.

 


Re: Can not access object from extended class

Posted by Christian Grobmeier <gr...@gmail.com>.
Mike,

the "Elder" constructor is not called by default in PHP.

You must use


function __construct() {
       parent::__construct();
...


in your "Young" class, then it should work.

Cheers


On Fri, Sep 2, 2011 at 5:19 PM, Michael Sole <ms...@gold-mobile.com> wrote:
> Ivan,
>
> I think the problem may have to do with the fact we are using a
> spl_autoload_register to call our classes and then we are calling the
> logger by manually including the class file into the other class files.
>
> Can you provide me with a code example that shows the right way of doing
> this?
>
> Thanks,
> Mike
>
> -----Original Message-----
> From: Ivan Habunek [mailto:ivan.habunek@gmail.com]
> Sent: Thursday, September 01, 2011 2:29 AM
> To: Log4PHP User
> Subject: Re: Can not access object from extended class
>
> On 31.8.2011. 19:09, Michael Sole wrote:
>> Actually this is how I have it:
>> http://pastebin.com/1Z1EcQcZ
>>
>> And the error is:
>> Fatal error: Call to a member function info() on a non-object in
>>
>> And in this case it would be on this line:
>> $this->log->info("From within");
>
> The example you provided works ok on my machine.
>
> The error says that you do not have an object in $this->log. Can you
> double check what is stored in $this->log after you initialize it? E.g.
> use a var_dump just after line 11.
>
> It should contain a Logger object at that point.
>
> Regards,
> Ivan
>
>
>
>
>
>



-- 
http://www.grobmeier.de

RE: Can not access object from extended class

Posted by Michael Sole <ms...@gold-mobile.com>.
Ivan,

I think the problem may have to do with the fact we are using a
spl_autoload_register to call our classes and then we are calling the
logger by manually including the class file into the other class files.

Can you provide me with a code example that shows the right way of doing
this?

Thanks,
Mike

-----Original Message-----
From: Ivan Habunek [mailto:ivan.habunek@gmail.com] 
Sent: Thursday, September 01, 2011 2:29 AM
To: Log4PHP User
Subject: Re: Can not access object from extended class

On 31.8.2011. 19:09, Michael Sole wrote:
> Actually this is how I have it:
> http://pastebin.com/1Z1EcQcZ
>
> And the error is:
> Fatal error: Call to a member function info() on a non-object in
>
> And in this case it would be on this line:
> $this->log->info("From within");

The example you provided works ok on my machine.

The error says that you do not have an object in $this->log. Can you
double check what is stored in $this->log after you initialize it? E.g. 
use a var_dump just after line 11.

It should contain a Logger object at that point.

Regards,
Ivan






Re: Can not access object from extended class

Posted by Ivan Habunek <iv...@gmail.com>.
On 31.8.2011. 19:09, Michael Sole wrote:
> Actually this is how I have it:
> http://pastebin.com/1Z1EcQcZ
>
> And the error is:
> Fatal error: Call to a member function info() on a non-object in
>
> And in this case it would be on this line:
> $this->log->info("From within");

The example you provided works ok on my machine.

The error says that you do not have an object in $this->log. Can you 
double check what is stored in $this->log after you initialize it? E.g. 
use a var_dump just after line 11.

It should contain a Logger object at that point.

Regards,
Ivan






RE: Can not access object from extended class

Posted by Michael Sole <ms...@gold-mobile.com>.
Actually this is how I have it:
http://pastebin.com/1Z1EcQcZ

And the error is:
Fatal error: Call to a member function info() on a non-object in

And in this case it would be on this line:
$this->log->info("From within");

Mike

-----Original Message-----
From: Ivan Habunek [mailto:ivan.habunek@gmail.com] 
Sent: Wednesday, August 31, 2011 12:54 PM
To: Log4PHP User
Subject: Re: Can not access object from extended class

On 31.8.2011. 18:46, Michael Sole wrote:
> I have an application that uses a base Database class and then each 
> class extends that. This gives easy database access to all the classes

> and insures that proper logging is maintain by all my developers.
>
> If I create a new instance of the database class directly the logging 
> works fine, when I create a new instance of the class that extends my 
> database class I get an error when access any of the log4php log
methods.
>
> I can add the getLogger method to each of the parent classes but this 
> is less than ideal. Is the a way to make log4php methods available 
> given the use cases I just outlined?

This should not be a problem. I use a similar pattern at work and it
works fine. Which error do you get?

Make sure the member variable you keep the Logger object in is
protected, and not private. Otherwise, the child object has no access.

Have a look at this proof-of-concept:
http://pastebin.com/rsMSQ6cg

Best regards,
Ivan

Re: Can not access object from extended class

Posted by Ivan Habunek <iv...@gmail.com>.
On 31.8.2011. 18:46, Michael Sole wrote:
> I have an application that uses a base Database class and then each
> class extends that. This gives easy database access to all the classes
> and insures that proper logging is maintain by all my developers.
>
> If I create a new instance of the database class directly the logging
> works fine, when I create a new instance of the class that extends my
> database class I get an error when access any of the log4php log methods.
>
> I can add the getLogger method to each of the parent classes but this is
> less than ideal. Is the a way to make log4php methods available given
> the use cases I just outlined?

This should not be a problem. I use a similar pattern at work and it 
works fine. Which error do you get?

Make sure the member variable you keep the Logger object in is 
protected, and not private. Otherwise, the child object has no access.

Have a look at this proof-of-concept:
http://pastebin.com/rsMSQ6cg

Best regards,
Ivan