1

I know this has been asked before and I have looked at multiple threads on why this would happen but could not understand how mine does not work as other variables defined in the same way and gathered the same way work.

class item{
    var $name = "test";
    var $id = 3;

    function setId($newID){
        global $id;
        $id = $newID;
    }

    function getId(){
        return $GLOBALS['id'];
    }

    function setName($newName){
        global $name;
        $name = $newName;
    }

    function getName(){
        return $GLOBALS['name'];
    }
}

That is a snippet of the class as it is really long but duplicate getName and setName for 5/6 more items.

$item[0]->getName();

Would return "test"

$item[0]->getId();

Returns the "Undefined index: id in link to file on line 59" which is the getId() function.

Every function other then getId() works and I have no idea why

EDIT - this question has been answered I am waiting to be able to accept an answer. $this worked for the variable to return (even though I'm still not sure why it would even though the 5 or 6 others would)

Austin
  • 47
  • 7
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – aynber Jan 16 '17 at 14:49
  • 3
    Why are you using `$GLOBALS` and not `$this`? Why are you `global`'ing in a class? – ʰᵈˑ Jan 16 '17 at 14:50
  • Might I suggest reading up on [variable scopes](http://php.net/manual/en/language.variables.scope.php). Inside a class, use `$this->` to access the class variables, don't set them to global. – aynber Jan 16 '17 at 14:50
  • As i said in the first line *I know this has been asked before and I have looked at multiple threads* I can not see how the answers there reflect what is going here at all - the variables are not being called from with in the class $this would not work, or do you mean to set the variable? – Austin Jan 16 '17 at 14:51
  • `var` is PHP 4 which is dead and buried years ago. Take a look at the [documentation of PHP classes and objects](http://php.net/manual/en/language.oop5.php). – axiac Jan 16 '17 at 14:55
  • `really long but duplicate getName and setName` you mean, these methods are declared more than once in the same class? And where are the instances created that you using in `$item[0]->getId()`? – JustOnUnderMillions Jan 16 '17 at 14:59
  • And about this: `Returns the "Undefined index` You never set the `$GLOBALS['id']` in the posted code here. plz check you code logic!! – JustOnUnderMillions Jan 16 '17 at 15:01
  • @JustOnUnderMillions as far as I can tell the two gets and sets above are printed the exact same way other then the variable name so I'm not sure how i set one and not the other. This has been answered waiting on accept timer to be able to select one – Austin Jan 16 '17 at 15:04

3 Answers3

5

Your understanding of variable scope within a class is wrong. Setters and getters are, usually, to modify and fetch private or protected properties. Assigning a property with var automatically gives them a public scope.

In a class, you don't need to use global - in fact, using global is bad practice anyway. You can reference class properties with $this.

For example;

class item {
    private $name = "test";
    private $id = 3;

    function setId($newID){
        $this->id = $newID;
    }

    function getId(){
        return $this->id;
    }

    function setName($newName){
        $this->name = $newName;
    }

    function getName(){
        return $this->name;
    }
}
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0
  1. Start your classes from large letter (and use camelCase for variables and CamelCase for classes):

    class Item

  2. Do not use var $id inside of your class, use private or protected access modifiers:

    private $id;

  3. Access your class variables using $this:

    function setId($id){ $this->id = $id; }

Hope it will help

degr
  • 1,559
  • 1
  • 19
  • 37
  • Point 1 *"Start your classes from large letter"* isn't needed, unless you're conforming to some sort of guidelines. It will work with the class name being lowercase. – ʰᵈˑ Jan 16 '17 at 15:00
  • point 2 is not needed too, and point 3 is not needed too. It all can work in any code style. Also it can work without classes and without function, and also it possible to do database queries from html code. But it will be called shitcode, and bring unfortune to coder. Then he will be cursed and I hope will die in torture. – degr Jan 16 '17 at 18:06
0

Not sure if this helps but I can't tell if you initialized the class at all.

<?php
class Item{   //Use Capital for class name
    var $name = "test";
    var $id = 3;

    function setId($newID){
        $this->id = $newID;
    }

    function getId(){
        return $this->id;
    }

    function setName($newName){
        $this->name = $newName;
    }

    function getName(){
        return $this->name;
    }
}

Then this is how you get name

$item = new Item(); //initialize 
$item->getName(); //get name
Steven Johnston
  • 1,729
  • 13
  • 16