Re: class variables and methods

From: Date: Sun, 19 Aug 2001 02:34:26 +0000
Subject: Re: class variables and methods
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-63312@lists.php.net to get a copy of this message
Joe, There are varying opinions on this issue. The argument for methods is that it hides the internal structure of your class. This is always a good thing. This means that the internals of the class can be changed, but the user interface (the user is the programmer here) does not change. So this alternative implementation of someclass class someclass { var $data = false; function set($key,$val) { $this->data[$key] = $val; } } is no different the the calling code. In general, I tend to have an accessor/mutator method for each 'property' that I would like to provide access to. I'd have a method for each $key that I want to support (this may not apply in your case). class someclass { var $name = false; var $age = false; function name() { if(func_num_args()) $this->name = func_get_arg(0); return $this->name; } function age() { if(func_num_args()) $this->age= func_get_arg(0); return $this->age; } } Now, this gets a big annoying to code since the lions share of my classes have the name method implementations. I've been hunting for a way to create methods dynamically, but I've only found one. To create class definitions on the fly and eval() them. This works great when you've got a huge number of properties (class/instance vars). I'll construct a static method on the class that processes examines the class vars (get_class_vars()) and defines a class extending my implementation. The sub class has the methods implemented for accessing/mutating the properties I'd like to have methods for. If anyone knows of a means to do this to an existing class (say using create_function() or something), I'd love to hear about it. Andy On Sat, Aug 18, 2001 at 07:09:08PM -0700, Joe Sheble (Wizaerd) wrote: > I've been doing some reading up on OOP in PHP, and looking at some of the > classes available on the web, and I've got what is probably a stupid > question. I've seen this many many times.. > > class someclass { > var $somevar = "someval"; > > function set( $key, $val ) { > $this->$key = $val; > } > } > > $myClass = new someclass; > $myClass->set( "somevar", "someotherval" ); > > why write a set method when you can just as easily call > $myClass->somevar = "someotherval"; > > Is there a reason for this? > > Also, alot of the different table classes (an OOP approach to HTML tables) > stores the data in arrays, but at what point is it too much to store into an > array? Say you have a forum and the text entered for a single respone could > be quite lengthy, much less for 100 replies... wouldn't that be a bit much > to store into an array? I know it depends on the system resources, but is > there a practical limit that people follow? > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > -- -------------------------------------------------- Andrew Libby Director of Technology CommNav, Inc alibby@commnav.com

« previous php.general (#63312) next »