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

Cardiner
  • 89
  • 1
  • 14

1 Answers1

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;
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • 1
    How is making property static is preferable solution in PHP? Not to speak about hardcoded error messages in class. – Leri Oct 21 '13 at 15:15
  • 1
    @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`. – newfurniturey Oct 21 '13 at 15:18
  • 1
    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. – Leri Oct 21 '13 at 15:24
  • Thx for great answer :) i chose making errorMsg static, because i am using this function in many cases as static call! – Cardiner Oct 21 '13 at 16:12
  • @Cardiner Well, you are doing it wrong... At some point, you'll realize that. ;) – Leri Oct 22 '13 at 07:15