Loading ...
Sorry, an error occurred while loading the content.

Re: Generic or individual functions

Expand Messages
  • Shekar C. Reddy
    Hi Hasin, Earlier, I was accessing (assign/reference) the variables directly in my classes. After writing individual methods for each var in the classes, I
    Message 1 of 4 , May 7, 2006
    • 0 Attachment
      Hi Hasin,

      Earlier, I was accessing (assign/reference) the variables directly
      in my classes. After writing individual methods for each var in the
      classes, I notice a degradation in performance. Here are some
      observations on XP SP-2 and GX270 with 3GHz CPU and 512 MB RAM:

      Page 1 (47 fields):
      Direct var access => 1.14 secs
      Method-based var access => 1.90 secs

      Page 2 (13 fields):
      Direct var access => .54 secs
      Method-based var access => 1.06 secs

      The application is large and object-oriented with a bunch of
      classes. I counted the vars in my classes and they are over 64
      (overall) that are now being accessed through methods in addition to
      some other vars that are accessed directly, data, lookup data, form-
      processing vars - all stored in arrays (passed by reference where
      required). We use Smarty with caching. I'm concerned from the point
      of view of a high-traffic website as for the application's
      performance. I have no idea as to how it would perform with this
      level of degradation as the site is not live yet.

      Do you have any tips on improving performance? PHP 5, MySQL 5 on
      Apache 2. Please advise...

      Thanks and regards,



      --- In phpexperts@yahoogroups.com, "Hasin Hayder" <phpfive@...>
      wrote:
      >
      > Hi
      > Definitely getVar($key) is not a good approach. You know PHP5 has
      some
      > builtin magic function. Two of them are __GET and __SET. You can
      use
      > them. See the following example
      >
      > class abc
      > {
      > private $val =
      > array("name"=>"shekhar","email"=>"shekhar@...");
      >
      > public function __get($var)
      > {
      > return $this->val[$var];
      > }
      >
      > public function __set($var, $value)
      > {
      > $this->val[$var] = $value;
      > }
      >
      > }
      >
      > Now you can use them like this
      >
      > $abc = new abc();
      > echo $abc->name; //display "Shekhar";
      > $abc->name = "Hasin";
      > echo $abc->name; //now Hasin
      >
      > Thats it.
      >
      > But this is not the answer of your question, from the point of
      > maintanability, the old get/set approach is still better than any
      > other aproach. the magic method __get() and __set() are just to
      reduce
      > the pain of writing so much get/set at once.
      >
      > Thanks
      > Hasin Hayder
      >
      > --- In phpexperts@yahoogroups.com, "Shekar C. Reddy" <powerobject@>
      > wrote:
      > >
      > > I have an array of vars in a class and am wondering how to
      provide
      > > an API for them - whether to use two generic functions (set/get)
      or
      > > one individual function for each array element. Eg:
      > >
      > > $vars = array( 'userid' => $userid,
      > > 'password' => $password,
      > > 'nickname' => $nickname,
      > > 'address' => array( 'street' => $street,
      > > 'city' => $city,
      > > 'state' => $state,
      > > 'zip' => $zip ),
      > > 'gender' => $gender,
      > > 'age' => $age,
      > > ...
      > > ... );
      > >
      > > function getVar( $key )
      > > { return $this->vars[ $key ); }
      > >
      > > function setVar( $key, $value )
      > > { $this->vars[ $key ] = $value; }
      > >
      > > OR:
      > > ===
      > >
      > > function getUserId()
      > > { return $this->vars[ 'userid' ]; }
      > >
      > > function setUserId( $userid )
      > > { $this->vars[ 'userid' ] = $userid; }
      > >
      > > function getPassword()
      > > function setPassword()
      > > function getAddress()
      > > function setAddress()
      > > ...
      > > ...
      > >
      > > The $vars array has over 20 elements. Which approach would be
      neater
      > > and recommended from the point of view of long-term
      maintainability.
      > >
      > > Thanks,
    Your message has been successfully submitted and would be delivered to recipients shortly.