ballhogjoni Posted October 6, 2008 Share Posted October 6, 2008 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 More sharing options...
R0bb0b Posted October 6, 2008 Share Posted October 6, 2008 The word "new" returns the object, not necessarily the constructor's return value. Link to comment https://forums.phpfreaks.com/topic/127240-solved-php-object-question/#findComment-658126 Share on other sites More sharing options...
ballhogjoni Posted October 6, 2008 Author Share Posted October 6, 2008 How can I return the 'constructor's return value'? Link to comment https://forums.phpfreaks.com/topic/127240-solved-php-object-question/#findComment-658131 Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127240-solved-php-object-question/#findComment-658132 Share on other sites More sharing options...
R0bb0b Posted October 6, 2008 Share Posted October 6, 2008 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 ?> Link to comment https://forums.phpfreaks.com/topic/127240-solved-php-object-question/#findComment-658141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.