how to properly initialize a new class and pass a value to use for it's methods

Say I have the following class:

class Item{
    public $itemid;
    public $itemattribute;

    function __construct($itemid){
        $this->itemid = $itemid;
    }
    public function getData(){
        $this->itemattribute = foo($itemid);
    }
}

Then I'm initiating a new object with this code:

$item = New Item($itemid);
$item->getData();
var_dump($item);

Basically what I am trying to do, is define the $itemid when I initialize the object, and when I call $item->getData, have it perform that function, using the previously defined $itemid. This is my first class and attempt writing OOP from scratch, so bear with me. The problem is that the $itemid isnt being passed to the foo function within getData.

Answers


The approach seems right (Except you need to use $this-> to access the property), but: Do yourself a flavour and have a look at how getter and setter are normaly used. A getter should return something, while a setter sets something. If you need to do "something" internal, you should use another method, or even do it in the constructor, etc..

class Item{
    public $itemid;
    public $itemattribute;

    function __construct($itemid){
        $this->itemid = $itemid;
    }

    public function getItemId(){
        return $this->itemid;
    }

    //option 1:) 
    public function calculateAttributeValue(){
       $this->itemAttribute = foo($this->itemid);
    }

    //option 2:)
    public function calculateAttributeValue($func){
       $this->itemAttribute = $func($this->itemid);
    }

    //option 3:) (lazy loading)
    public function getItemAttribute(){
       if ($this->itemAttribute == null){
           $this->itemAttribute = foo($this->itemid);
       }

       return $this->itemAttribute;
    }

    //option 4 (eager loading)
    function __construct($itemid){
        $this->itemid = $itemid;
        $this->itemAttribute = foo($this->itemid);
    }

    public function getItemAttribute(){
       return $this->itemAttribute;
    }
}

However it is a GOOD idea, to either use eager OR Lazy loading. Whenever you need to "call" another function in order to compute a value, BEFORE a getter will return a value, your design becomes sensitive to "calling the functions in the right order", which will be bad for large Classes, and even worse, if one calculation depends on another calculation done prior...

Bad usagE:

$myClass = new Item(5);
$myClass->CalulateA();
$myClass->CalculateB();
$myClass->getB();

Better:

$myClass = new Item(5); //calculates A and B
$myClass->getB();

or

$myClass = new Item(5);
$myClass->getA(); //calculates A.
$myClass->getB(); //calculates B and A when required.

to call the itemid in your function simply load it inside the function.

class Item{
 public $itemid;
 public $itemattribute;

 function __construct($itemid){
    $this->itemid = $itemid;
 }
 public function getData(){
    $itemid = $this->itemid; // or 
    // simply. do  what ever you want to itemid
    $this->itemid;

   return  $this->itemattribute = foo('bar');
   // you should have return

 }
}

You need to pull the value with $this->itemid and not $itemid in your getData function in order to "pass" the value to the function:

class Item{
    public $itemid;
    public $itemattribute;

    function __construct($itemid){
        $this->itemid = $itemid;
    }
    public function getData(){
        // echo $this->itemid; // This will print the same value you passed to the constructor
        $this->itemattribute = foo( $this->itemid); // <-- HERE
    }
}

Note that if you expect a value from getData(), you'll need to return something:

public function getData(){
    $this->itemattribute = foo( $this->itemid);
    return $this->itemattribute;
}

After you create the object:

$item = New Item($itemid);

$item->itemid takes the value of $itemid according to your constructor.

So just use $item->itemid in your getData() method


Need Your Help

Allocating a struct dirent without malloc()

c memory-management memory-alignment readdir strict-aliasing

I need to use readdir_r() to read the contents of a directory in a multithreaded program. Since the size of struct dirent is filesystem dependent, man readdir_r recommends

CSS @supports vs Checking Support via JS

javascript css css3 browser-feature-detection

While searching for a way to check CSS support in browsers I came across two different approaches: CSS and JavaScript. I have used JS before for checking feature support in browsers, but not for CSS

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.