Jump to content


Photo

Fatal Error: Call To A Member Function Seterror() On A Non-Object In

php

  • Please log in to reply
6 replies to this topic

#1 raamay

raamay

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 19 December 2012 - 05:39 AM

hello, i have a Form Class as below:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
	
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'configuration.php' );
	
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
	
class Form
{
	 var $values = array();
	 var $errors = array();
	 var $num_errors;
	
	 function Form(){
	
		 if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
	
			 $this->values = $_SESSION['value_array'];
			 $this->errors = $_SESSION['error_array'];
			 $this->num_errors = count($this->errors);
	
			 unset($_SESSION['value_array']);
			 unset($_SESSION['error_array']);
		 }
		 else{
	
			 $this->num_errors = 0;
		 }
	 }
	
	 function setValue($field, $value){
	
		 $this->values[$field] = $value;
	
	 }
	
	 function setError($field, $errmsg){
	
		 $this->errors[$field] = $errmsg;
	
		 $this->num_errors = count($this->errors);
	
	 }
	
	 function value($field){
	
		 if(array_key_exists($field,$this->values)){
	
			 return stripslashes($this->values[$field]);
	
		 }else{
	
			 return "";
	
		 }
	
	 }
	
	 function error($field){
	
		 if(array_key_exists($field,$this->errors)){
	
			 return "<font size=\"1\" color=\"#ff0000\">".$this->errors[$field]."</font>";
	
		 }else{
	
			 return "";
	
		 }
	
	 function getErrorArray(){
	
		 return $this->errors;
	
	 }
}
}

Now i am trying to use it in a joomla model for server-side validation. I have included and instantiated the class as below:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
JLoader::register('Form', dirname(__FILE__) . DS .'form.php');
include_once (JPATH_COMPONENT.DS.'models'.DS.'form.php');

if (class_exists('Form')) {
	 $form = new Form();
	 }
jimport('joomla.application.component.model');
	
Class JobsModelJobs extends JModel {
.
.
.
.
function validate(){
		 global $form;
	
		 $name = JRequest::getVar('name', 'POST');
		 $email = JRequest::getVar('email', 'POST');
	
		 $field = "name";
		 if(!$name || strlen($name = trim($name)) == 0){
			 $form->setError($field, "Please enter your name!");
		 }
		 $_SESSION['value_array'] = JRequest::get( 'POST' );
		 $_SESSION['error_array'] = $form->getErrorArray();
	
	 }	
}

After this, I have tried to invoke the function setError() and getErrorArray() of the Form class in validate() function of the model but i get the error "Fatal error: Call to a member function setError() on a non-object in".

Can anybody tell me where i have gone wrong or what i am missing?

The issue is relevant to this post http://forums.phpfre...-error-problem/. The only diffrerence is that i am not using session class and relying on joomla.

Edited by raamay, 19 December 2012 - 05:48 AM.


#2 Muddy_Funster

Muddy_Funster

    Advanced Member

  • Members
  • PipPipPip
  • 2,993 posts

Posted 19 December 2012 - 06:21 AM

try putting an else on the if(class_exists('Form')) that will warn you the class hasn't been found. see if that warning comes up.
A simple password hash :

function makePass($word=''){
  $dbSalt = '$2a$07$'.substr(hash('whirlpool',$word),0,22);
  $dbPass = crypt($word, $dbSalt);
 return substr($dbPass,12);
}



My SQL/PHP Blog

#3 raamay

raamay

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 19 December 2012 - 07:15 AM

try putting an else on the if(class_exists('Form')) that will warn you the class hasn't been found. see if that warning comes up.

i have tried to use
echo $form->num_errors;
$form->setError("name", "Enter name");
echo $form->error("name");
inside the class_exists() and i get the "0" and "Enter name" as the output which means the class is working before it is used inside the model class.

i tried the else statement also but it doesn't get executed. class_exists() returns true.

Edited by raamay, 19 December 2012 - 07:19 AM.


#4 trq

trq

    Advanced Member

  • Administrators
  • 30,731 posts
  • LocationSydney, Australia.

Posted 19 December 2012 - 07:33 AM

You should pass this object into your method via an argument. Globals break the encapsulation provided.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 raamay

raamay

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 20 December 2012 - 04:35 AM

You should pass this object into your method via an argument. Globals break the encapsulation provided.

can you cite an example please!

#6 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 20 December 2012 - 05:24 AM

http://php.net/manua...s.arguments.php

Works the same for regular functions as it does for functions (methods) within a class.

Edited by Christian F., 20 December 2012 - 05:24 AM.

Keeping it simple.

#7 kareem3d

kareem3d

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 23 January 2013 - 11:24 AM

What trq meant is to modify the method validate to accept a $form argument like this
function validate($form)
and remove the line 'global $form;'

And when calling the validate method pass the $form object to it.

Edited by kareem3d, 23 January 2013 - 11:24 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com