Jump to content

[SOLVED] php object question


ballhogjoni

Recommended Posts

I have this class and I want to call a function within it when I instansiate the class. My problem is that when I return the value and print it to the screen I get commentcheck Object ( ). I believe I should get something that looks like an array. Any advice/help?

 

<?php
session_start();
class CommentCheck {
function CommentCheck( $post ){
	$sErrorMessage = CommentCheck::captchaCheck( $post );
	print_r($sErrorMessage); //this actually prints correctly on screen
	return $sErrorMessage; //it seems that the error message is not being returned
}
function captchaCheck( $post ){
	include("/home/xxxxxxxxxxxxx/common_scripts/comments_script/comments/includes/comments-config.php");
	include("/home/xxxxxxxxxxxxxxxx/common_scripts/comments_script/comments/includes/clean_input.php");
	$secure_match = strtoupper( trim( $post['secure_match'] ) );
	if ( $secure_match != $_SESSION['captcha'] ){
		$image = FACS_href."articles/images/responses/bad-captcha.gif";
		$size = @getimagesize($image);
		$sErrorMessage = '<img class=\'right\' align="absmiddle" src="'.$image.'" '.$size[3].' />Your security entry does not match the security image. Please take a closer look and try again.';
		return $sErrorMessage;
	}
}
}
?>

 

code that should print the error message:

$oCommentCheck = new CommentCheck($_POST);
	print_r($oCommentCheck);//this prints commentcheck Object ( )

Link to comment
https://forums.phpfreaks.com/topic/127240-solved-php-object-question/
Share on other sites

Yes, you're not creating an object. You need to do this:

 

<?php
$CommentCheck = new CommentCheck();
$oCommentCheck = $CommentCheck->CommentCheck($_POST);
	print_r($oCommentCheck);//this prints commentcheck Object ( )
?>

 

Plus, I don't think you can use the same name for a function that you are already using for your class name.

Constructors are not suppose to return values, I usually just create a get function:

<?php
function getErrorMessage()
{
return $this->sErrorMessage;
}
?>

<?php
$CommentCheck = new CommentCheck();
	print_r($CommentCheck->getErrorMessage());//this prints the error array
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.