Generic Item and Collection classes

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Generic Item and Collection classes

Post by Technical »

I'm quite new to this concept, so can you please tell me is this a good realisation?

IItem Interface:

Code: Select all

interface IItem
{
	function Load();
	function Create();
	function Fill();
	function GetId();
	function Exist(Identifier $Identifier);
	function Exists();
}
Item Class:

Code: Select all

class Item implements IItem
{
	protected $Id;
	protected $Exists = FALSE;
	function __construct()
	{
		$Count = func_num_args();
		if($Count == 1 && func_get_arg(0) instanceof Identifier)
		{
			$this->Load(func_get_arg(0));
		} else {
			call_user_func_array(array($this, 'Create'), func_get_args());
		}
	}
	function Load() { }
	function Create() { }
	function Fill() { }
	function GetId()
	{
		return $this->Id;
	}
	function Exist(Identifier $Identifier)
	{
		$this->Id = $Id->Value;
		$this->Exists = TRUE;
	}
	function Exists()
	{
		return $this->Exists;
	}
}
ICollection Interface:

Code: Select all

interface ICollection
{
	function Listed();
	function Parameters();
	function Exists($Id);
	function Length();
}
Collection Class:

Code: Select all

class Collection implements ICollection
{
	public $Items = array();
	function __construct()
	{
		$Count = func_num_args();
		if($Count == 1 && func_get_arg(0) instanceof Identifiers)
		{
			$this->Listed(func_get_arg(0));
		} else {
			call_user_func_array(array($this, 'Parameters'), func_get_args());
		}
	}
	function __set($Name, $Value) { }
	private function Listed(Identifiers $Identifiers) { }
	private function Parameters() { }
	function Exists($Id)
	{
		return !empty($this->Items[$Id]);
	}
	function Length()
	{
		return count($this->Items);
	}
}
Last edited by Technical on Sun Jan 23, 2011 11:24 am, edited 1 time in total.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Generic Item and Collection classes

Post by greyhoundcode »

I'm asking only out of curiosity, but why have an interface with private methods?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Generic Item and Collection classes

Post by John Cartwright »

greyhoundcode wrote:I'm asking only out of curiosity, but why have an interface with private methods?
I've actually never come across anyone implementing an interface with anything other than public methods. It is usually up to the interface to determine the public methods, and the programmer to determine the inner workings.

I guess it would make sense if your classes are abstracted and you need to ensure they have certain internal methods, but I just don't see any reason to have to define private methods in your interface.

As to the classes themselves, I'm quite confused. What are the Listed(), Parameters(), Exist(), and Exists() methods for?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Generic Item and Collection classes

Post by greyhoundcode »

John Cartwright wrote:I guess it would make sense if your classes are abstracted and you need to ensure they have certain internal methods, but I just don't see any reason to have to define private methods in your interface.
That's what I was thinking. In C# for instance (unless I'm much mistaken) it isn't legal to define interface members as anything other than public - but I'm always interested to hear the thinking/rationale.

(Edit)
To clarify, rationale of why the OP decided to place private members in the interface, that is.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Generic Item and Collection classes

Post by Technical »

John Cartwright wrote:
greyhoundcode wrote:I'm asking only out of curiosity, but why have an interface with private methods?
I've actually never come across anyone implementing an interface with anything other than public methods. It is usually up to the interface to determine the public methods, and the programmer to determine the inner workings.

I guess it would make sense if your classes are abstracted and you need to ensure they have certain internal methods, but I just don't see any reason to have to define private methods in your interface.

As to the classes themselves, I'm quite confused. What are the Listed(), Parameters(), Exist(), and Exists() methods for?
Actually, I meant they are abstract classes, I never use them as they are, only for inheritance.

As for those methods:

1) Listed() method creates collection with specified IDs. For example, if I want to get information about all authors:

Code: Select all

$Authors = new UserCollection(new Identifiers($ContentCollection->Authors));
2) In the other hand, Parameters() fills collection with items by specified parameters, like count or search string:

Code: Select all

$UserCollection = new UserCollection(0, 10, 'abc'); // 10 users with 'abc' search string
3) Exist() method forces item to set $Exists property to TRUE, thus ensure it loaded from database. It used when collection is being filled. You can't afford 20 queries to fill collection with 20 items, right?

4) Exists() method is pretty obvious I think. For Item it returns TRUE if the Item object was loaded from database. For Collection it checks if there is item with specified ID.
Last edited by Technical on Sat Jan 22, 2011 2:08 am, edited 1 time in total.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Generic Item and Collection classes

Post by Technical »

Can you please tell what is exactly wrong? I already made these classes abstract.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Generic Item and Collection classes

Post by Jonah Bron »

greyhoundcode wrote:... In C# for instance (unless I'm much mistaken) it isn't legal to define interface members as anything other than public ...
Same in Java.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Generic Item and Collection classes

Post by Technical »

Fixed
Post Reply