If you really need to know the size of the array without wanting to simply access the array directly and do a count() as needed, you could just create a method, so it's always up-to-date any regardless of any changes made to the array:
PHP Code:
class myClass extends base {
private $productIDs = array(12, 78, 79, 153); // change to public if you really want direct read/write access
/**
* Use this when you just want the count without actually accessing the array
*/
public function getCount()
{
return count($this->prodcutIDs);
}
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
The "function" is actually a method that's a member of a class yet you are trying to call it like a regular function. Instead you need to instantiate the class and then call the method from that object.
but i need that variable, the array length, for another function that needs to be defined within the class. can you advise?
i like yer getCount function, thanks
You can either call that method internally whenever you need it, or just do a $count=count($this->hazMatProdsOne) itself within the method where it's needed. If you really feel it's desirable to have the count as a separate variable (for now I've not seen any reason to, as far as my programming tastes go), you could populate it via your constructor method:
PHP Code:
class Foo { private $hazMatProdsOne = array(115,162); private $hazMatProdsTwo = array(49, 83, 82); private $countHazMatProdsOne; private $countHazMatProdsTwo; public function __construct() { $this->countHazMatProdsOne = count($this->$hazMatProdsOne); $this->countHazMatProdsTwo = count($this->$hazMatProdsTwo); } }
Of course, if we're really going to get all OOP about this, it might be that each of those "hasMatProds" arrays should actually be objects, whose class might contain a count() method.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks