Jump to content

Fatal error: Method name must be a string


ShaolinF

Recommended Posts

Hi Guys

I am trying to make a validation class but I keep getting errors when trying to sanitize my $_POST vars. See code followed by error:

 

<?php
require_once('FormValidator.php');

class MyClass
{
	public $validator;

	__constructor()
	{
		$this->$validator = new FormValidator();
	}

	function useValidation()
	{
		$value = $this->$validator->sanitizeStr($_POST['MyString']);
	}
}
?>

<?php
// Form Validator Class
class FormValidator
{
	function sanitizeStr($value)
	{
		return filter_var($value, FILTER_SANITIZE_STRING);
	}
}
?>

 

ERROR MESSAGE:

Fatal error: Method name must be a string in C:\wamp\www\wordpress\wp-content\plugins\my-plugins\php\classes\MyClass.php on line 34
LINE 34: $value = $this->$validator->sanitizeStr($_POST['MyString']);

your code should be something like below your variable accessing convention your using is wrong

 

<?php
   require_once('FormValidator.php');
   
   class MyClass
   {
      public $validator;
      
      public function __construct()
      {
         $this->validator = new FormValidator();
      }
      
      public function useValidation()
      {
         $this->validator->sanitizeStr($_POST['MyString']);
      }
   }
?>

Hi Guys

I am trying to make a validation class but I keep getting errors when trying to sanitize my $_POST vars. See code followed by error:

 

<?php
   require_once('FormValidator.php');
   
   class MyClass
   {
      public $validator;
    
/*
   The error is caused by the line below... I added "public function " and the 
   magic method is
   __construct *not* __constructor
*/
      public function __construct()
      {
         $this->$validator = new FormValidator();
      }
      
      function useValidation()
      {
         $this->$validator->sanitizeStr($_POST['MyString']);
      }
   }
?>

<?php
   // Form Validator Class
   class FormValidator
   {
      function sanitizeStr($value)
      {
         return filter_var($value, FILTER_SANITIZE_STRING);
      }
   }
?>

 

ERROR MESSAGE:

Fatal error: Method name must be a string in C:\wamp\www\wordpress\wp-content\plugins\my-plugins\php\classes\MyClass.php on line 34
LINE 34: $this->$validator->sanitizeStr($_POST['MyString']);

Thanks guys. So why isn't the following technique not following conventions:

 

$value = $this->$validator->sanitizeStr($_POST['MyString']);

 

You need to remove the $ so it reads

 

$value = $this->validator->sanitizeStr($_POST['MyString']);

 

<?php
   require_once('FormValidator.php');
   
   class MyClass
   {
      public $validator;
   
      public function __construct()
      {
         $this->validator = new FormValidator();
      }
     
      function useValidation()
      {
         $this->validator->sanitizeStr($_POST['MyString']);
      }
   }
?>

 

Also noticed you used $validator in the construct...

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.