Skip to main content
added 983 characters in body
Source Link
newfurniturey
  • 38.6k
  • 10
  • 99
  • 104

Your function validateText() is a static function; because of this, it doesn't belong to a single "instance" of the class Validation but instead to all of them and, therefore, is not applicable to the $this keyword.

Your choices here are to either drop static from the function declaration or to make $errorMsg static itself (which, based on it's definition may be a good way to go):

public static $errorMsg = array( 1000 => 'Some Error', 1001 => 'Some other error'); static function validateText($value) { if (!empty($value)) { if (ctype_alpha($value)) { return false; } else { return Validation::$errorMsg[1001]; //getting error here } } else { return Validation::$errorMsg[1001];//getting error here } } 

Add-on (const versus static)
Based on recommended comments, I am also adding in the "appropriate" way to handle your exact situation. The above will fix your error, however, it is not the best way to approach "error messages" as class-properties. Instead of using static, you can setup a list of constant class members using the const keyword (which won't work with arrays, so you'll be creating several variables here instead):

class Validation { const SOME_ERROR = 'Some Error'; const SOME_OTHER_ERROR = 'Some other error'; static function validateText($value) { // process value return Validation::SOME_ERROR; } } 

You can also access these constants from outside of the Validation class via: Validation::SOME_ERROR and, if you have PHP 5.3+, you can use:

$v = new Validation(); echo $v::SOME_ERROR; 

Your function validateText() is a static function; because of this, it doesn't belong to a single "instance" of the class Validation but instead to all of them and, therefore, is not applicable to the $this keyword.

Your choices here are to either drop static from the function declaration or to make $errorMsg static itself (which, based on it's definition may be a good way to go):

public static $errorMsg = array( 1000 => 'Some Error', 1001 => 'Some other error'); static function validateText($value) { if (!empty($value)) { if (ctype_alpha($value)) { return false; } else { return Validation::$errorMsg[1001]; //getting error here } } else { return Validation::$errorMsg[1001];//getting error here } } 

Your function validateText() is a static function; because of this, it doesn't belong to a single "instance" of the class Validation but instead to all of them and, therefore, is not applicable to the $this keyword.

Your choices here are to either drop static from the function declaration or to make $errorMsg static itself (which, based on it's definition may be a good way to go):

public static $errorMsg = array( 1000 => 'Some Error', 1001 => 'Some other error'); static function validateText($value) { if (!empty($value)) { if (ctype_alpha($value)) { return false; } else { return Validation::$errorMsg[1001]; //getting error here } } else { return Validation::$errorMsg[1001];//getting error here } } 

Add-on (const versus static)
Based on recommended comments, I am also adding in the "appropriate" way to handle your exact situation. The above will fix your error, however, it is not the best way to approach "error messages" as class-properties. Instead of using static, you can setup a list of constant class members using the const keyword (which won't work with arrays, so you'll be creating several variables here instead):

class Validation { const SOME_ERROR = 'Some Error'; const SOME_OTHER_ERROR = 'Some other error'; static function validateText($value) { // process value return Validation::SOME_ERROR; } } 

You can also access these constants from outside of the Validation class via: Validation::SOME_ERROR and, if you have PHP 5.3+, you can use:

$v = new Validation(); echo $v::SOME_ERROR; 
Source Link
newfurniturey
  • 38.6k
  • 10
  • 99
  • 104

Your function validateText() is a static function; because of this, it doesn't belong to a single "instance" of the class Validation but instead to all of them and, therefore, is not applicable to the $this keyword.

Your choices here are to either drop static from the function declaration or to make $errorMsg static itself (which, based on it's definition may be a good way to go):

public static $errorMsg = array( 1000 => 'Some Error', 1001 => 'Some other error'); static function validateText($value) { if (!empty($value)) { if (ctype_alpha($value)) { return false; } else { return Validation::$errorMsg[1001]; //getting error here } } else { return Validation::$errorMsg[1001];//getting error here } }