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)

8
  • 2
    Possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Jan 16, 2017 at 14:49
  • 3
    Why are you using $GLOBALS and not $this? Why are you global'ing in a class? Commented Jan 16, 2017 at 14:50
  • Might I suggest reading up on variable scopes. Inside a class, use $this-> to access the class variables, don't set them to global. Commented Jan 16, 2017 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? Commented Jan 16, 2017 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. Commented Jan 16, 2017 at 14:55

3 Answers 3

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

Comments

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

2 Comments

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

Comments

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.