/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP object method error

Here is what my script basically looks like:

[CODE]<?php
require_once( PATH . ‘/includes/ADP_Posts.class.php’ );
if ( have_posts() ) {
if ( is_single() ) {
$sb_posts = new ADP_Posts( ‘type=rel&postID=’ . $post->ID . ‘&numPosts=3&period=week’ );
$sb_postsTitle = ‘Related’;
if ( ! $sb_posts->hasPosts() ) {
$sb_posts = new ADP_Posts( ‘type=ran&numPosts=3’ );
$sb_postsTitle = ‘More News’;
}
} else {
$sb_posts = new ADP_Posts( ‘type=ran&numPosts=3’ );
$sb_postsTitle = ‘More News’;
}
}
?>
<!– do some html stuff –>
<?php if ( $sb_posts->hasPosts() ) {
// do some php stuff with the $sb_posts object
} ?>
<!– continue html stuff –>[/CODE]

When $sb_posts->hasPosts() is called the second time, the script runs as expected, but I keep getting:

[14-Oct-2008 14:00:18] PHP Fatal error: Call to a member function hasPosts() on a non-object in /thescript.php on line 35

in my error log, reported twice at 2 second intervals every time the script is called and only when the method is called again on line 35

I just can’t figure this one out, I’ve tried stting the $sb_posts variable to null and using the unset( $sb_posts ) but I don’t know

if it matters all $sb_posts->hasPosts() does is return a 1 or 0 value based on the length of an array

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 14.2008 — Which line is line 35? The error message is telling you that at that point $sb_posts is not an object. My suspicion is that the first if() condition is failing, so the ADP_Posts object is never instantiated, but that's only a guess right now. Perhaps you need to move the instantiation of $sb_posts to outside of that if block?
Copy linkTweet thisAlerts:
@tsliceauthorOct 14.2008 — [CODE]34-> <!-- do some html stuff -->
35-> <?php if ( $sb_posts->hasPosts() ) {
36-> // do some php stuff with the $sb_posts object[/CODE]


I still don't get it. I assume the object instantiates because lines soon after line 35 within the line-35 if-block calls methods of the object without error.
Copy linkTweet thisAlerts:
@NogDogOct 14.2008 — That's the line I would expect to fail if the preceding IF condition fails. As to why it works later, I can't say without seeing all the code (not that I necessarily want to, mind you). Without knowing what the process logic is that you want there, all I can suggest is that perhaps you need to change that section to something like:
[code=php]
<?php if ( isset($sb_posts) && $sb_posts->hasPosts() ) {
// do some php stuff with the $sb_posts object
} ?>
[/code]
Copy linkTweet thisAlerts:
@tsliceauthorOct 15.2008 — Sometimes both if blocks fail but only the second block throws an error.

The only thing I can think of that's screwing with me is the fact that I'm re-constructing the same object on the same var with different constructor params. It would make more sense to construct the object once and use a new method of the object to re-populate the object with data when the if block fails.

I'm not sure if this is correct, but I think it makes more sense than what I'm currently doing, so I'll give it a try and see if it fixes the problem.

For now it works and is not killing my app, but my log is getting ugly.
Copy linkTweet thisAlerts:
@ariellOct 15.2008 — Try to make it a Singleton class. That way you can be sure to always dealing with the SAME object instance.

Also, for debugging purposes, it's always a good idea to THROW an error deliberately at an appropriate place within the class, best Constructor. You'll get an error trace, which is convenient to track down the problem.

Best from the south.
Copy linkTweet thisAlerts:
@tsliceauthorOct 16.2008 — As I expected, by adding a new method to my class that allowed me to re-populate the $sp_posts object with data instead of re-constructing the object fixed the problem I was having.

OLD ?

[CODE]class ADP_Posts
{
private $posts = array();

public function __construct( $args )
{
$this->posts = // get posts script
}
}[/CODE]


NEW ?

[CODE]class ADP_Posts
{
private $posts = array();

public function __construct( $args )
{
$this->get_posts( $args );
}

public function get_posts( $args )
{
$this->posts = // get posts script
}
}[/CODE]
×

Success!

Help @tslice spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...