You are here: Retrieving an object from an array « PHP Programming Language « IT news, forums, messages
Retrieving an object from an array

Posted by Ramon on 06/09/06 17:26

Hello there,

I'm quite new to OOP in PHP. Unfortunately, I'm forced to work with PHP4,
because my website is still hosted with this version.

I've made two classes (see below): "Product" and "ProductList".
Class "Product" has some properties, including an ID prop to store it's
database ID.
Class "ProductList" contains an array of "Product" objects.

My question is this:

Is there some fancy way to retrieve a Product object from the ProductList's
object by it's ID-property, without looping through the Products in the
object array?
(Please note: I need to find it by a property value, not by it's array
index!)

Thanks!
Ramon


== code ========================================

class ProductList {
var $m_oProductList = array();

function add ($oProduct) {
$this->m_oProductList[] =& $oProduct;
}

function getProductByID ($iID) {
return $this->m_oProductList[ ??? ]; <-- and this is where I got
stuck...
}
}


class Product {
var $m_iID;
var $m_sName;

function Product ($iID=0) {
$this->m_iID = $iID;
}

function setID ($iID) {
$this->m_iID = $iID;
}

function getID () {
return $this->m_iID;
}

function setName ($sName) {
$this->m_sName = $sName;
}

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация