Retrieving an object from an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramon

    Retrieving an object from an array

    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 "ProductLis t".
    Class "Product" has some properties, including an ID prop to store it's
    database ID.
    Class "ProductLis t" 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_oProductLi st[] =& $oProduct;
    }

    function getProductByID ($iID) {
    return $this->m_oProductLi st[ ??? ]; <-- 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;
    }
    }


  • Colin McKinnon

    #2
    Re: Retrieving an object from an array

    Ramon wrote:
    [color=blue]
    >
    > I've made two classes (see below): "Product" and "ProductLis t".
    > Class "Product" has some properties, including an ID prop to store it's
    > database ID.
    > Class "ProductLis t" 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!)
    >[/color]

    Why not use the property value *as* the array index.

    <snip>[color=blue]
    > function add ($oProduct) {
    > $this->m_oProductLi st[] =& $oProduct;
    > }[/color]

    function add(&$oProduct, $id=false) {
    if ($id===false) {
    $id=$oProduct->m_iID;
    } else {
    $oProduct->m_iID->setID($id);
    }
    $this->m_oProduct[$id]=$oProduct;
    }

    (I think the reference operator is redundant - like Perl, PHP arrays only
    seem to hold scalars, so the value is assigned to the array by reference).

    HTH

    C.

    Comment

    • Ramon

      #3
      Re: Retrieving an object from an array

      Thanks for your tips Colin!

      greeting,
      Ramon.


      "Colin McKinnon"
      <colin.thisisno tmysurname@ntlw orld.deletemeun lessURaBot.com> wrote in
      message news:Vhiig.2381 7$rC1.13515@new sfe4-gui.ntli.net...[color=blue]
      > Ramon wrote:
      >[color=green]
      >>
      >> I've made two classes (see below): "Product" and "ProductLis t".
      >> Class "Product" has some properties, including an ID prop to store it's
      >> database ID.
      >> Class "ProductLis t" 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!)
      >>[/color]
      >
      > Why not use the property value *as* the array index.
      >
      > <snip>[color=green]
      >> function add ($oProduct) {
      >> $this->m_oProductLi st[] =& $oProduct;
      >> }[/color]
      >
      > function add(&$oProduct, $id=false) {
      > if ($id===false) {
      > $id=$oProduct->m_iID;
      > } else {
      > $oProduct->m_iID->setID($id);
      > }
      > $this->m_oProduct[$id]=$oProduct;
      > }
      >
      > (I think the reference operator is redundant - like Perl, PHP arrays only
      > seem to hold scalars, so the value is assigned to the array by reference).
      >
      > HTH
      >
      > C.
      >[/color]


      Comment

      • David Haynes

        #4
        Re: Retrieving an object from an array

        Ramon wrote:[color=blue]
        > class ProductList {
        > var $m_oProductList = array();
        >
        > function add ($oProduct) {
        > $this->m_oProductLi st[] =& $oProduct;[/color]
        $this->m_oProductLi st[$oProduct->getID()] =& $oProduct;
        // don't know if you need =& here, = may be enough[color=blue]
        > }
        >
        > function getProductByID ($iID) {
        > return $this->m_oProductLi st[ ??? ]; <-- and this is where I got
        > stuck...[/color]
        return $this->m_oProductLi st[$iID];[color=blue]
        > }
        > }[/color]

        should work. [Caveat: it's Friday... ;-) ]

        -david-

        Comment

        Working...