Jump to content

mich2004

New Members
  • Posts

    8
  • Joined

  • Last visited

Everything posted by mich2004

  1. mich2004

    safety

    Bert you need to be sanitising your user input for sure. The following (filter_input) should help.. http://www.php.net/manual/en/function.filter-input.php
  2. Hi Guys, still new to OOP. What I'm trying to achieve is quite straightforward and I've had some success, however, it feels "wrong" how I'm implementing the classes I have created which makes me feel I have a design problem or there is a better way to achieve what I want which right now I cannot see. The task is quite clear, I receive a numerical user_id, after setting up the DB connection I pass it to a factory which executes a few queries and then generates an array of objects, the array will hold the objects relevant to what the person has been, i.e. has been a player, has been a coach, referee or whatever (when I pass the id I obviously have no idea of the role a person has held, it may be player only, it could be player and coach, etc). My person class is extended to specific roles, player, referee, coach etc and obviously the array of returned objects enables me to access the properties required. The issue is during implementation I obviously need to know what objects I have as to what methods I can call. So attempting $my_person->refGame will throw an error if $my_person is only holding a player object. So my imlementation is along the lines of this the moment.. $my_person= PersonFactory::createPerson($database, $person_id); // my_person can be an array of objects, getName from parent class valid for all objects $details = $my_person[0]->getName(); // We need to know what class we have returned to call specific methods... foreach($my_person as $dis_array){ if (get_class($dis_array) == 'Player'){echo "I've been a player"; $my_person[0]->playerMethod; etc} if (get_class($dis_array) == 'Manager'){echo "I've been a manager"; $my_person[1]->somemanagerMethod; etc} if (get_class($dis_array) == 'Referee'){echo "I've been a Ref"; $my_person[2]->refMethod} } Really using the if block to identify the classes returned feels wrong. I feel like I'm missing something here, how should I be handling a returned array of objects in implementation? Also accessing each object via $my_person[0] (player) or $my_person[1] (manager) is wrong in implementation. Any help much appreciated.
  3. Thanks all, I was having a bit of a head spinning day when I posted! I'd sortof adapted a solution using Neil's method (passing db to constructor of team class), but of course the missing link for me was creating a static method and in turn making the team constructor private.
  4. Hi guys, I don't know if I'm over complicating things here and cannot see the wood for the trees, but I seem to have got myself confused. What I'm simply trying to do is get teamName and teamresults of a team from a database. The code will perhaps explain better than I can (note I haven't included the DB class which is fine). class TeamCollection { protected $database; protected $teamid; protected $team_name; // pass db by dependency injection... public function __construct(Database $database) { $this->database = $database; } public function getTeam($teamid){ $this->team_id = $teamid; $this->database->query("SELECT team_name, nickname, founded FROM club WHERE team_id=:teamid"); $this->database->bind(':teamid', $this->team_id); // spawn a new Team object if query is valid, if not throw exception and end via try/catch... if ($result = $this->database->single()) { return new Team($result); // create new Team passing $result to Team constructor } else { throw new exception ('No Valid Team returned'); } } public function getResults($teamid){ $this->team_id = $teamid; $this->database->query("SELECT result FROM results WHERE team_id=:teamid"); $this->database->bind(':teamid', $this->team_id); // If a Team has valid results, return array of match scores.. if ($scores = $this->database->results()) { return $scores; } else { throw new exception ('No Valid Team Results to return'); } } } //end class // Team is abstracted from DB class Team { private $team_name; private $result; private $scores; public function __construct(array $result) { $this->team_name = $result['team_name']; } public function getName() { $this->team_name; } public function getResults(array $scores) { foreach ($scores as $row){ echo $row; } } } // implementation $team_id =9; // passed variable.. $database = new Database($server,$db_type); // create db.. $database->connect(); // connect to db.. $collection = new TeamCollection($database); // pass live DB link to TeamCollection Class try { $team = $collection->getTeam($team_id); // try and spawn a valid Team Object.. echo $name = $team->getName(); // If so lets return a Team name $scores = $team->getResults($team_id); // Now try and get Team Results.. $display_results = $scores->getResults($scores); // Display results by passing them to Team class to } catch (Exception $e) { Echo 'Exception Thrown: ' . $e->getMessage(); } My basic difficulty is where to put the getResults method and how to use it? I'm fine spawning a new Team as required, but is my logic for then moving on to getResults sound? I was trying to abstract my Team class from having any database association at all. Is this a wise approach or just not required? Am I overcomplicating the issue or missing something? Can anyone help? I guess my difficulty is between connecting classes and objects in the best manner. Thanks in advance.
  5. Guys, as you can imagine I'm pretty confused by now with so many replies suggesting so many different things. Can we start again. Is my initial code on the right tracks? I don't quite follow the "having the class and implementation of the class in the same script is not appropriate" comment. I can see the coupling of the class is too tight, but to be honest I'm just lost from there with so many suggestions making different points!
  6. I wanted to thank everyone for the help. Seems there is a lot more reading for me to do. While I do understand Adam's singleton approach, I must admit some of the other methods are over my head at this stage. I'll go and read some more.
  7. Hi Adam, thanks for this, I do generally follow what you have written and I have managed to implement it as a test. Can I ask one further question. Being passed to my implementation are two variables $server and $db_type, up to this point I have been passing these to the constructor of my DB class to determine the connection credentials required (i.e. different db or password for live/local server etc.), i.e. $database = new Database($server,$db_type). However, If I wanted to pass the same variables now, I'm not sure how. It doesn't seem appropriate to pass these via the Team object at all. So what is the best way using the singleton method to be able to pass variables to the DB constructor, is this even possible or advised? Your help would be appreciated.
  8. Hi Guys, I'm new to OOP. I've mastered some basic syntax, but am wondering about an issue of design. To clarify in this example, I'm simply looking to pull Team data from a Database (I've not included my db class (PDO wrapper) although I know it is working. Although I have seen this sort of method below in a book (I believe and on the net) and it does work, I cannot help thinking coupling the DB class so tightly with the Team Class isn't a great approach. Can anyone give me feedback as to whether my approach is valid? While I can see the negative issue of tight coupling (i.e. changes to any database method would require multiple changes in Team (as team methods were added), I cannot really see an alternative way of doing this? I guess this is an issue of a design pattern or more advance OOP. Can anyone suggest an alternative way and/or more reading on making the coupling looser while still achieving my objectives? Should, for example, all DB functionality in getName be done during implementation? I initally thought pushing all db related functionality inside a Team method was wise and the best way then to add further methods, i.e. getResults method would be similar to getName but obviously with a different query and processing afterwards inside Team, but now I wonder if all should be in the implementation, or is there another approach? Thanks in advance. // create team class class Team { private $db; private $team_name; private $team_id; private $result; // in constructor lets pass DB object public function __construct($db) { $this->db = $db; } //function to get team name public function getName($team_id) { $this->team_id = $team_id; $this->db->query("SELECT team_name, nickname, founded FROM club WHERE team_id=:teamid"); $this->db->bind(':teamid', $this->team_id); if ($result = $this->db->single()); { return $result[]; } } }// end class // Implementation $team_id=1; //passed from user input // First Create Db Object with correct passed variables $database = new Database($server,$db_type); // Invoke DB connect method $database->connect(); // Now create Team object, pass it database object $team = new Team($database); // call Team method $team_display = $team->getName($team_id); // close db $database->closedb();
×
×
  • 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.