Jump to content

MelodyMaker

Members
  • Posts

    23
  • Joined

  • Last visited

    Never

Everything posted by MelodyMaker

  1. I think you're right for the method that changes the caption. But for adding and removing photos?
  2. So the operations such as: -add -remove -move (by "moving", I mean that the photo number 7 can be moved to position 1 or 2 and the other photos (their numbers) must be ordered consequently. -getPhotoByNumber (which returns a Photo instance, by passing it his number) Should be left out of the user class? But then, should the user class store an array of photos or not? Since Photos are associated to User by a idUser property, they can be left out. Or am I wrong? From a logical point of view it seems also correct to me to store them in the user object since a foto belongs to a user...
  3. I'm sorry, I don't want to bother you with my questions but a User has: -a name -an email - a country ID ... as private properties : Class User{ private name; private email; private countryID; ... } so, why it should not contain an array of photos and addresses as properties as well? Is it because they are arrays?
  4. Thanks keeb, for your help. The fact is that I see Photos, Description and Addresses as aggregates of a user. I mean, I think they're related each other, or I 'm wrong?
  5. Hi, I would like to know if there's a way to avoid bad repetition like this one: I have a User class: class User{ private $conf; private $photos = Array(); private $addresses = Array(); private $descriptions = Array(); ... public function __construct($userData){ $this->conf = Configuration::getInstance(); ... } public function addPhoto(Photo $photo){ $photoNumber = $this->getPhotosAmount(); if( $photoNumber < $this->conf->userMaxPhotos){ $photo->setIdUser($this->u_idUser); $photo->setNumber(++$photoNumber); $this->photos[] = $photo; return true; } return false; } public function addDescription(Description $description){ $descNumber = $this->getDescriptionsAmount(); if( $descNumber < $this->conf->userMaxDescriptions){ $description->setIdUser($this->u_idUser); $description->setNumber(++$descNumber); $this->descriptions[] = $description; return true; } return false; } public function addAddress(Address $address){ $addressNumber = $this->getAddressesAmount(); if( $addressNumber < $this->conf->userMaxAddresses){ $address->setIdUser($this->u_idUser); $address->setNumber(++$addressNumber); $this->addresses[] = $address; return true; } return false; } ... } Of course, there are also three "remove" functions. It looks like very bad, to me... As you can see, the methods are very similar and this is very annoying. So, I decided to create and abstract class called "profileItem" which is a superclass of Photo, Description, and Address Class. The abstract ProfileItem Class: abstract class AbsProfileItem{ private $number = 1; private $idUser = 0; public function getIdUser(){ return $this->idUser; } public function setIdUser($idUser){ $this->idUser = $idUser; } public function getNumber(){ return $this->number; } public function setNumber($number){ if($number > 0){ $this->number = $number; } } } The Photo class: class Photo extends AbsProfileItem { private $url = ""; private $caption = ""; public function __construct($url,$caption){ $this->url = $url; $this->caption = $caption; } public function getUrl(){ return $this->url; } public function setUrl($url){ $this->url = $url; } public function getCaption(){ return $this->caption; } public function setCaption($caption){ $this->caption = $number; } } Address and Descriptions are similar... The problem is that I don't know how to integrate them with the user class...I mean, I need, for example an addItem method which can be used for Description, Photo, and Address. But at some point, there will be a conditional statement, something like: if ($item instance of "Address") then "the address should be added to $this->addresses if ($item instance of "Photo") then "the photo should be added to $this->photos if ($item instance of "Description") then "the description should be added to $this->descriptions ... Is there a clean, elegant way to do this (maybe with the help of a pattern...don't know)? Thank you very much in advance! Davide
  6. I'll do it for sure. Thank you very much to all of you!
  7. Oh, and someone told me to also introduce into the system a UserManager that creates user instances by passing a $idUser to his method: $userManager = UserManager::getInstance(); $user = userManager->getUserById($idUser); I know that this is much cleaner, but is that the only reason to introduce all those "managers"? Thank you very much in advance. Davide
  8. Thank you very much for your suggestions! What I maybe still don't understand (and I hope this is due to the fact that I'm a beginner) is to introduce the Gallery (or PhotoManager object). I mean, why? Loading, adding photos, and so on, cannot be simply methods of the user class? Or this violates the encapsulation principle? Is it for that reason?
  9. ...but the AddPhoto and DeletePhoto are still in user class? And your method: public function getAllPhotos(User $user) { $userId = $user->getId(); $query = "SELECT * FROM userphotos WHERE userId = '$userId'"; $result = mysql_query($query); return $result; } This should build and return Photo instances, right?
  10. Yes, some people told me to go that way as well. So, we should have three classes involved: a Photo Class a PhotoRepository Class a User Class. Photo Class which models a photo: Class Photo{ function __construct($url,$number,$caption){ ... } } a PhotoRepository Class which has (or stores) a User reference: Class PhotoRepository{ public function __construct(User $user) //<- is it correct to pass it as a constructor parameter? {...} public function getAllPhotos(){ //Here the query to the DB $QUERY = "SELECT * FROM userphoto WHERE idUser='.$this->user->getIdUser()'."; ... $arr = Array(); while($res = ...){ $arr[] = new Photo($res[url], $res['caption'],$res['number'] } return $arr; } //What about deleting a photo??? (from the repository AND FROM THE DB?) } and the class User: class User{ function __construct($id){ ... } //The following methods should call a PhotoRepository method? public function Addphoto() { ... } public function removePhoto() { } }
  11. Thanks for your answer! The getPhotos method you wrote works only if first a user adds a photo. But sometimes, I have to load all the photos from the database, without using the method add. This is this point my question came from: function getPhotos(){ SELECT * from userphoto where idUser = ... //at this point, I have to build the object photo, but how? while ($res = $result->fetch_array()){ //here I can get the IDPhoto, since i retrieve it from the DB (the link (url) to the foto is in the DB and the file has uploaded) $photo = new Photo ($res['IDPhoto'] $res, $res['caption']) // } BUT, for example, when a user, uploads a photo (if the upload has success) the photo can't have a IDPhoto, because it gets (or should get) that ID ONLY after a successful update AND after an INSERT into a DB. So, a constructor like this: new Photo ($IDPhoto,$url, $caption) in the case of an upload does not work, I think. It is this that is making me crazy... }
  12. Thanks again, and I'm sorry if I ask you another question, but this is very very important for me. There's still something I don't understand. You wrote the photo Class: class Photo { private $url; private $caption; public function __construct($url, $caption) { $this->url = $url; $this->caption = $caption; } public function upload(User $user) { $userId = $user->getId(); $query = "INSERT INTO photos (url, caption) VALUES ({$this->url}, {$this->caption}) WHERE user_id = '$userId'"; $result = mysql_query($query); if($result) { //success! } else { //failure } } } The constructor gets a url and a caption as arguments. That's ok. What is making me mad, now, is that I would like to have a method that gives me all the photo for a specific user and also a metod to delete a photo. The first problem is: where should I place those methods? Something tells me that they should be in the user Class: class User{ ... public function getAllPhotos(){ $QUERY = "SELECT * FROM userphoto WHERE idUser=..." $result = mysql_query($QUERY); while($res = $result->fetch_array()){ $photo = new Photo($res['url'],$res['caption']) $this->photos[] = $photo } } } Is this correct? Now the problem: The delete method. It should not only remove a photo from the "photos" array of the user class, but also delete it from database (and maybe even delete it from disc). For this reason, I think that it would be nice to have also a IDPhoto value somewhere (which is infact the primary key value of the table userphoto). Otherwise, the SQL DELETE statement would be really bad: DELETE FROM userphoto WHERE idUser= and url=... best would be: DELETE FROM userphoto WHERE IDPhoto = $IDPhoto. I don't know how to insert IDPhoto, since we construct the class only with url and caption. And last, I will certainly use also a "number" field to number the photos, a user has. I don't know if this could be use to delete the photos, instead of using a IDPhoto.. Thanks in advance, I know I'm asking you a lot.Sorry if I bother you... Davide
  13. Just a quick question: passing a User instance as argument to the upload photo method, is it correct from a formal point of view?
  14. Thank you! The fact is that I really can't figure out, how the two classes are associated, I mean: let's suppose that I give the user (it is so) the ability to add (upload) a photo and delete a photo. Then, according to what you say, these methods should be part of the Photo Class. which parameters should I pass to the AddPhoto? A reference to the user that owns that photo. So, first I have to instantiate User class, then the foto class (what about the parameters in its constructor?) and then, somewhat pass the userId to the photo Class? Please not that my photo has only four attributes: a url, a caption, an idUser and a number (plus the methods to upload and delete, if I put them here). Sorry, I know that I'm terribly confused...
  15. Sorry if I bother you with this, but I'm a OOP newbie and I would like to know your opinion about that. I'm developing a site which requires registration and where users can upload their fotos. The question is this: I created a user class class User{ private $username; private $email; private $country; ???private $photos= Array();??? ... function __construct($idUser) //here I load the details about the user; } Now, every user can upload at most 12 photos, which can have a small caption. So, the question: 1) Do I need a "Photo" Class? (the encapsulation principle tells me so) In this case what is the best way to associate this class to the user class? By storing the photo objects in an array property of the user class? And where should I put the method "loadPhotos"? In the user or in the photo class? class Photo(){ private $url; private $caption; function __construct($IDPhoto){ .... here another select on the same table, mmm } } ...but if I do this, there will be two SELECT : One to get the user's ID photo and the other to retrieve photo's data. Or I'm wrong..? 2) Storing all the data about photos in an array (as a property) and forget about Photo class? I'm a bit confused, sorry. Thank you very much in advance. Davide
  16. HI! I'm a bit confused about the associations between objects, so I would like to ask you about it, by using the following example: class DatabaseManager{ private function __construct(){} private function __clone(){} static public function getInstance(){ switch (Configuration::getInstance()->DBMS){ case "mysql": return MySqlDB::DBgetInstance(); break; default: return MySqlDB::DBgetInstance(); } } } Class Configuration{ private $DBconn = "localhost|myUser|myPwd|myDB"; private $sessionExpiration = 3600; private $DBMS = "mysql"; private static $confInstance; private function __construct(){} public static function getInstance(){ if (!self::$confInstance){ self::$confInstance = new Configuration(); } return self::$confInstance; } public function __GET($property){ if(isset($this->$property)){ return $this->$property; } return false; } private function __clone(){} } Class MysqlDB{ private $conf; public function __construct(){ $this->conf = Configuration::getInstance(); ... } ...bla bla } I just would like to know what kind of association ther is between I just would like to know what kind of association ther is between the configuration and the other two classes. I've just started to draw UML diagrams, and I'm getting mad about it... Thanks in advance! Davide
  17. Thank to all of you. Let's suppose that it is NOT a Database object that you want to pass across the pages of,say, a website and use in objects, but anything else (for example a Log or Error object). Is there a way to change only the class (base class) and, of course, his name without changing every instantiation across the script ($newObject = new newObject()...) something like this, I mean: interface BaseObject | Obj1 OBj2 OBj3 .... (all implement the interface and this objects are interchangeable, I could use Obj1 or Obj2, or Obj3...) | Then, a CurrentObject that someway (which?) stores or get the Object I need (Obj1 or Obj2, or Obj3,...) This way, I can keep the scripts untouched because I instantiate the CurrentObject: $newObject = new CurrentObject() // does not matter if this uses Obj1 or Obj2 or Obj3... I don't know if the question is clear, I hope so. Thanks in advance. Davide
  18. Yes, you're right. I'm sorry for that, what I was trying to explain is that the constructor is private because I use a singleton pattern to instantiate the object. Sorry, I put a wrong comment there... Anyway, at this point I wonder what are abstract classes useful for. After reading that article about Java and derived classes it seems that abstract classes (and, in general, base classes) should be replaced by interfaces. I'm a bit confused about that...
  19. Thank you very much. Could you tell me please if the following one could be a correct solution: abstract class Database{ abstract protected function DBQuery($query,$line="",$file=""); abstract protected function DBfetchArray($mode=""); abstract static protected function DBgetInstance(); ... } class mysqliDB extends Database{ private function __construct()... //this should be a singleton static public DBgetInstance() ... //this returns only an Instance of this object public function DBQuery ... public function DBFetchArray ... } class SessionManager{ //or any other object that neeeds a DB connection public function loadDB (Database $db){ //type hint .... } } $session = new SessionManager(); $session->loadDB(mysqliDB::getInstance()); //and so on for any other objects that need a DB Could this be a correct solution?
  20. Thank you. Here I found other informations about DAO: http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html It looks like a bit complicated to me, but I surely will consider it.
  21. Hi, I've just got into PHP Object-Oriented programming, and I wold like to ask to you the following question (I think, to many of you this is maybe a very stupid one): I have two classes: A Database class and a SessionManager class. So the question is quite simple: what is the best way to pass to SessionManager a DB instance? -passing it to the SessionManager constructor? $session = new SessionManager(Database::getInstance()); -passing it to a SessionManager method? $session = new SessionManager(); $DB = Database::getInstance() (suppose I use Singleton pattern) $session->loadDB($DB) or putting it directly INSIDE the SessionManager Object, maybe in its constructor: Class SessionManager{ private $DB; function __construct(){ $this->DB = Database::getInstance(); } } ... The questions comes from the fact that there will be many objects in my project and lots of them will use the DB object. I really don't know if there's a "best" solution. Maybe you can help. Thanks in advance. Davide
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.