0

I have this class:

<?php
    class Test {

        private $_ID;
        private $_NAME;
        private $_AGE;

        public function setID() { $this->_ID++; }
        public function getID() { return $this->_ID; }

        public function setNAME($element) { $this->_NAME = $element; }
        public function getNAME() { return $this->_NAME; }

        public function setAGE($element) { $this->_AGE = $element; }
        public function getAGE() { return $this->_AGE; }

        public function addUser($name, $age) {
            Test::setID();
            Test::setNAME($name);
            Test::setAGE($age);

            echo "OK";
        }
    }
?>

I want to create objects of this class, and assign the data with the function addUser like this:

$test = new Test();
$test:: addUser("Peter", "12"); but I have errors.

I have this errors:

Strict Standards: Non-static method Test::addUser() should not be called statically in /var/www/public/testManu.php on line 13

Strict Standards: Non-static method Test::setID() should not be called statically in /var/www/public/class/Test.php on line 18

Fatal error: Using $this when not in object context in /var/www/public/class/Test.php on line 8

I have problems with variable scope. Could somebody tell me what it is my problem????

3
  • 1
    can you tell a bit more about the problem? Commented Feb 9, 2015 at 11:09
  • "I have problems with variable scope" is too vague - what symptoms do you see? Errors? Unexpected behaviour? Any error messages? Commented Feb 9, 2015 at 11:10
  • what is Teprohelper? Commented Feb 9, 2015 at 11:11

3 Answers 3

2

change this:

 ...
 public function addUser($name, $age) {
        $this->setID();
        $this->setNAME($name);
        $this->setAGE($age);

        echo "OK";
    }
 ...

Calling like Classname::function() is only valid for static methods. You have a dedicated instance which need to be addressed with the construct $this->function().

And thus:

   ...
  $test->addUser("Peter", "12"); but I have errors.
Sign up to request clarification or add additional context in comments.

5 Comments

His class is called Test the other one he used is Teprohelper. So the usage is probable (you dont know what Teprohelper is, but with names its not the Test class therefore cannot be changed into $this;
I have corrected the description of my mistake, now is correct
@Seti Just an assumption that Teprohelper is miss typed and it should be the same class/object as you may see by the methods which are called - defined in class Test
@Seti yepp. He's messed up with object references
Yep, he fixed that, but still. What is usage of class that can store only one user at its best?
2
$test = new Test();
$test -> addUser("Peter", "12"); #now no errors

2 Comments

But still. Can you tell us what you want to achieve exactly? You are using two classes here. One Test and one Teprohelper...
IGNORE TEPSOHELPER IT'S A TYPO!
1

This should work for you:

<?php
    class Test {

        private static $_ID = 1;
        private static $_NAME;
        private static $_AGE;

        public static function setID() { self::$_ID++; }
        public static function getID() { return self::$_ID; }

        public static function setNAME($element) { self::$_NAME = $element; }
        public static function getNAME() { return self::$_NAME; }

        public static function setAGE($element) { self::$_AGE = $element; }
        public static function getAGE() { return self::$_AGE; }

        public static function addUser($name, $age) {
            self::setID();
            self::setNAME($name);
            self::setAGE($age);

            echo "OK";
        }
    }

$test = new Test(); /*You don't need to instantiate the class 
                      because you're calling a static function*/
$test:: addUser("Peter", "12"); but I have errors.
?>

4 Comments

I think its not what he wanted to achieve... Now he have one class that can store one user.... And thats all...
Should work, but the question is whether he actually wants static methods on a non-instance or an instance. As he's instantiating the object, I assume that it shouldn't be static.
I have this error: Parse error: syntax error, unexpected '++' (T_INC) in /var/www/public/class/Test.php on line 8
You need to assign a number to private static $_ID I thought you already had set it, I've updated my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.