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']);

Link to comment
Share on other sites

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']);
      }
   }
?>

Link to comment
Share on other sites

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']);

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.