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

asset_path - fonts not applied

ruby-on-rails ruby-on-rails-3 fonts asset-pipeline jruby

I am trying to load some additional fonts in my rails application using:

How do I change IE script debugger from MS Script Debugger to VS 2005

javascript internet-explorer visual-studio-2005 debugging

I have Microsoft Script debugger installed and selected (somwhere) as the script debugger of IE. I want to make VS the default script debugger. When I uninstall MS Script debugger IE just wouldn't ...

solr.extraction.ExtractingRequestHandler ClassNotFoundException

java solr apache-tika

I am working on an internal project in my company that requires solr, but I could not manage to link it to Tika. I bought the apache solr 4 cookbook yet I couldn't figure out the solution.

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.