Building a Base Class
Moderator: General Moderators
Building a Base Class
So my base controller class and my base model class etc are all well underway, but I was thinking of establishing a base EVERYTHING class that all classes will inherit. I figured this would have some sort of elegant setter/getter functions, maybe some functions related to naming and abstract variables, but I can't tell if this is unnecessary complexity. Does anyone do this or has anyone seen it done? If so, what sort of generic functions would belong in such a class?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
It is my observation that this is not commonly done in PHP. It may seem like a good idea, but there really does not end up being any common functionality you want in all of your classes. It does seem a rather brilliant idea when you think about it, but in practice it is not.
(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Building a Base Class
^^^ What he said
Re: Building a Base Class
I agree as well. A base class ends up as a default place to dump common functionality where you can't find the right scope to place it in. This just means you need to think about it a little more instead of giving up and creating a generic base class.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Building a Base Class
I agree, but I always come back to other OO languages like Java, JavaScript, Objective-C.arborint wrote:It is my observation that this is not commonly done in PHP. It may seem like a good idea, but there really does not end up being any common functionality you want in all of your classes. It does seem a rather brilliant idea when you think about it, but in practice it is not.
They all have a base class of Object (or other loose/generic description) and that base class contains some really important methods like equals(Object otherObject), toString() and hashCode().
I think PHP should have adopted a similar approach where everything extends from stdClass or something. Sometimes equals() and hashCode() methods come in useful when dealing with sets of objects.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
I agree, although with == and === is does have funcitonailty like equals(). And PHP does have __toString() (I really wish they would add __toArray() ! ). hashCode() would be nice but you would also need the collection classes (HashMap, HashTable, ...).Chris Corbyn wrote:They all have a base class of Object (or other loose/generic description) and that base class contains some really important methods like equals(Object otherObject), toString() and hashCode().
I have to say Chris that I am really disappointed that you didn't want wait() and notify() added to PHP objects! Theory? could do some crazy interesting things with those!
(#10850)
Re: Building a Base Class
SPL's ArrayIterator has getArrayCopy() which returns an array representation of the object - http://www.php.net/~helly/php/ext/spl/c ... 21e8f2ea1a
Re: Building a Base Class
Even with equals you're going to need a different implementation for value / entity objects, and even then the methods aren't going to be used for things like controllers, utility classes, etc...
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
That's interesting to know, thanks! But that does what get_object_vars() already does. Really you want __toArray() to be like __toString() where you can control what is returned when the object is use in an array context.pytrin wrote:SPL's ArrayIterator has getArrayCopy() which returns an array representation of the object - http://www.php.net/~helly/php/ext/spl/c ... 21e8f2ea1a
(#10850)
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Building a Base Class
Isn't that essentially what the SPL Iterator does? Or are you talking about something else?arborint wrote:Really you want __toArray() to be like __toString() where you can control what is returned when the object is use in an array context.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
Yes it is. It's just a just a big clunky interface with a bunch of methods. I think it would be nicer to just have:allspiritseve wrote:Isn't that essentially what the SPL Iterator does? Or are you talking about something else?
Code: Select all
public method __toArray() {
// your code here
return $somearray;
}(#10850)
Re: Building a Base Class
I agree that a __toArray would be really nice. I even recall writing a blog post on operator overloading some ways back requesting this very feature.
Re: Building a Base Class
A __toArray() method would not do the same thing as SPL. How is it "clunky"?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Building a Base Class
Using SPL you would normally have to code at a minimum:jshpro2 wrote:A __toArray() method would not do the same thing as SPL. How is it "clunky"?
Code: Select all
class Test2 extends ArrayIterator {
protected $data = array('one', 'two', 'three', 'four');
function key() {
return key($this->data);
}
function current() {
return current($this->data);
}
function next() {
next($this->data);
}
function valid() {
return current($this->data) !== false;
}
}Code: Select all
class Test1 extends ArrayIterator {
protected $data = array('one', 'two', 'three', 'four');
function __construct() {
parent::__construct($this->data);
}
}Compare that to:
Code: Select all
class Test1 {
protected $data = array('one', 'two', 'three', 'four');
function __toArray() {
return $this->data;
}
}(#10850)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Building a Base Class
I assume __toArray() would return an immutable array? I'm not sure how you'd handle assignments with __toArray(). __toString() is different since strings are always immutable.
$object['foo'] = 'bar';
$object['foo'] = 'bar';