Building a Base Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Building a Base Class

Post by Theory? »

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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

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

Post by alex.barylski »

^^^ What he said
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Building a Base Class

Post by Eran »

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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Building a Base Class

Post by Chris Corbyn »

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.
I agree, but I always come back to other OO languages like Java, JavaScript, Objective-C.

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

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 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, ...).

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)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Building a Base Class

Post by Eran »

SPL's ArrayIterator has getArrayCopy() which returns an array representation of the object - http://www.php.net/~helly/php/ext/spl/c ... 21e8f2ea1a
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Building a Base Class

Post by josh »

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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

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
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.
(#10850)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Building a Base Class

Post by allspiritseve »

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.
Isn't that essentially what the SPL Iterator does? Or are you talking about something else?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

allspiritseve wrote:Isn't that essentially what the SPL Iterator does? Or are you talking about something else?
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:

Code: Select all

public method __toArray() {
    // your code here
   return $somearray;
}
In most cases you don't really want it to be "iteratable," you just want it to return an array in an array context. The foreach() or array function you pass it to can just use the returned array. SPL Iterator maintains the current pointer in the array, but 99% of the time you just iterate over the entire array.
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Building a Base Class

Post by Eran »

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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Building a Base Class

Post by josh »

A __toArray() method would not do the same thing as SPL. How is it "clunky"?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Building a Base Class

Post by Christopher »

jshpro2 wrote:A __toArray() method would not do the same thing as SPL. How is it "clunky"?
Using SPL you would normally have to code at a minimum:

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;
    }
}
Although thinking about it you could also do:

Code: Select all

class Test1 extends ArrayIterator {
    protected $data = array('one', 'two', 'three', 'four');
    
    function __construct() {
        parent::__construct($this->data);
    }
}
But that unfortunately passes a copy of the array, so the class probably could not modify the array after instantiation -- which is not very handy.

Compare that to:

Code: Select all

class Test1 {
    protected $data = array('one', 'two', 'three', 'four');
    
    function __toArray() {
        return $this->data;
    }
}
Hopefully you can see that because most of the time your just want to do foreach($obj as $value) it makes the most common use case as trivial as __toString() is to implement. Plus you don't have to extend to get the functionality.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Building a Base Class

Post by Chris Corbyn »

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';
Post Reply