DAO -- should they be based on one table?

OK, so this is fairly basic, then. The object inside the other object is the same as any other property (or member variables, as the PHP doc calls them). The album class could be something like this:

class Album {
    var $title;
    var $artist;
    var $id;
    function Album($id,$title,$artist) {
          $this->title = $title;
          $this->id = $id;
          $this->artist = $artist;
    }

    function getArtist() {
          return $this->artist;
    }

    function somethingElseEtc...
     ...

}

I won’t complicate this with PHP 4-style “references”, mostly since I loathe them and I think you can get by without them in this case.

You can do this:

$artistname = $album->artist->name;

In PHP 5, you can use accessors in the same way:

$artistname = $album->getArtist()->getName();

In PHP 4, you have to do the get methods in sequence:

$artist = $album->getArtist();
$artistname = $artist->getName();

Or you can make a getArtistName method in the Album class that calls the Artist object.

Dagfinn, thanks a lot for this hint! :slight_smile:

Good examples Mkrz, want to see how I handle things ? :smiley:

Maybe compare notes huh ?

class NewsGateway {
		function NewsGateway() {
		}
		
		function FindByLimit( &$db, $limit ) {
			$findByLimit = "SELECT
				id,
				UNIX_TIMESTAMP(date) AS date,
				title,
				heading,
				body FROM news ORDER BY date DESC LIMIT ".$limit;
			
			$statement = $db -> Query( $findByLimit );
			#
			return $statement -> Execute();
		}
	}
	
	class NewsArchiver {
		var $ar;
		
		function NewsArchiver() {
			$this -> ar = array();
		}
		
		function SetId( $id ) {
			$this -> ar['id'] = $id;
		}
		
		function SetDate( $date ) {
			$this -> ar['date'] = $date;
		}
		
		function SetTitle( $title ) {
			$this -> ar['title'] = $title;
		}
		
		function SetHeading( $heading ) {
			$this -> ar['heading'] = $heading;
		}
		
		function SetBody( $body ) {
			$this -> ar['body'] = $body;
		}		
	}
	
	
	class FindLatestArchive extends NewsArchiver {
		function FindLatestArchive() {
			NewsArchiver::NewsArchiver();
		}
		
		function Invoke( $row ) {
			$this -> SetId( $row['id'] );
			$this -> SetDate( $row['date'] );
			$this -> SetTitle( $row['title'] );
			$this -> SetHeading( $row['heading'] );
		}
		
		function GetId() {
			return $this -> ar['id'];
		}
		
		function GetDate() {
			return $this -> ar['date'];
		}
		
		function GetTitle() {
			return $this -> ar['title'];
		}
		
		function GetHeading() {
			return $this -> ar['heading'];
		}
	}
	
	
	class FindLatest {
		function FindLatest() {
		}
		
		function Load( &$resultset ) {
			$indexer = array();
			$iterator = &new QueryIterator( $resultset );
			#
			while( $iterator -> IsValid() ) {
				$row = $iterator -> GetCurrent();
				$iterator -> GetNext();
				#
				$archive = &new FindLatestArchive;
				$archive -> Invoke( $row );
				#
				$indexer[] = &$archive;
			}
			return $indexer;
		}
	}
	
	
	class News {
		var $model;
		
		function News() {
			$this -> Execute();
		}
		
		function Fetch() {
			return $this -> model;
		}
		
		function Execute() {
			# create or fetch an instance of database
			$db = &db::getInstance();
			# implement model
			$this -> model = FindLatest::Load( NewsGateway::FindByLimit( &$db, 5 /* number of news items */ ) );
		}
	}

Im currently tossing up if I shall follow the dao/gateway/manager for users, groups and usergroups classes. This is because I found it easy to manage my reviews this way.