ShaolinF Posted November 22, 2009 Share Posted November 22, 2009 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']); Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/ Share on other sites More sharing options...
rajivgonsalves Posted November 22, 2009 Share Posted November 22, 2009 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']); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/#findComment-963246 Share on other sites More sharing options...
Andy-H Posted November 22, 2009 Share Posted November 22, 2009 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']); Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/#findComment-963247 Share on other sites More sharing options...
ShaolinF Posted November 22, 2009 Author Share Posted November 22, 2009 Thanks guys. So why isn't the following technique not following conventions: $value = $this->$validator->sanitizeStr($_POST['MyString']); Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/#findComment-963255 Share on other sites More sharing options...
Andy-H Posted November 22, 2009 Share Posted November 22, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/#findComment-963258 Share on other sites More sharing options...
ShaolinF Posted November 22, 2009 Author Share Posted November 22, 2009 EDIT: FIXED Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/#findComment-963265 Share on other sites More sharing options...
Andy-H Posted November 22, 2009 Share Posted November 22, 2009 Duno if the feature is still installed since the upgrade but there should be a "Topic Solved" button at the bottom of the page somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/#findComment-963270 Share on other sites More sharing options...
KevinM1 Posted November 22, 2009 Share Posted November 22, 2009 Duno if the feature is still installed since the upgrade but there should be a "Topic Solved" button at the bottom of the page somewhere? It hasn't been installed yet. Quote Link to comment https://forums.phpfreaks.com/topic/182503-fatal-error-method-name-must-be-a-string/#findComment-963278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.