-1

I can't access one of my class properties! Here is the code:

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

My log tells me this: PHP Fatal error: Using $this when not in object context

How can i access this array???

3
  • 5
    It's a static function, so there's no $this. Commented Oct 21, 2013 at 15:11
  • Read this post stackoverflow.com/questions/1957629/… Commented Oct 21, 2013 at 15:11
  • Oh, so stupid! I did not even notice that my function was static... SO SO STUPID! Commented Oct 21, 2013 at 16:09

1 Answer 1

1

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; 
Sign up to request clarification or add additional context in comments.

5 Comments

How is making property static is preferable solution in PHP? Not to speak about hardcoded error messages in class.
@Leri I'm slightly confused by your question; "how is making a property static a preferable solution" - this is a solution that's used in countless languages, PHP being one of them. This exact example, an array of error messages, is a good example of when/how to use a static variable in a class as it won't change between instances (not to mention the method itself is static). The alternative, which won't work with arrays, is to define a const (or, in this case, several const variables). However, to address the OP's actual problem, I stuck with static.
The main idea was that while this is solution and correct one it's not better than having instance variables. Because like that you can have multiple validators with different error messages. If you'd want to share data between instances, you could pass the same array to those instances. Also with static you create hidden dependencies all over your script.
Thx for great answer :) i chose making errorMsg static, because i am using this function in many cases as static call!
@Cardiner Well, you are doing it wrong... At some point, you'll realize that. ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.