Jump to content

ignace

Moderators
  • Posts

    6,457
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by ignace

  1. Yes these are mere examples. I'm not the one they hired to write the code for them.
  2. defined(!DB_USERNAME) is the same as defined(0) I think you mean !defined('DB_USERNAME') (mind the ') $result = $this->database->db_query("SELECT * FROM users WHERE id={$id}"); $final = mysqli_fetch_array($result); You either fully abstract your database or you don't, not trying to use both or you'll still end up screwed. The same goes for: __construct(MySqlDatabase $database) In OO we have a rule: program to an interface, not an implementation. Thus something like: interface DatabaseInterface { function connect($user, $pass, $host = 'localhost'); function disconnect(); function isConnected(); function prepare($sql); function execute($sql = null, $params = array()); function escape($value); } class MySQLDatabase implements DatabaseInterface { // implement the above methods } class UserGateway { private $db = null; function __construct(DatabaseInterface $db) { $this->db = $db; } function findById($id) { return new User( $this->db->select()->from('users')->where('id', $id); ); } function fetchAll($count = null, $offset = null) { if($count != null && $offset != null) { return new UserList( $this->db->select()->from('users')->limit($count, $offset); ); } else if($count != null) { return new UserList( $this->db->select()->from('users')->limit($count); ); } else { return new UserList( $this->db->select()->from('users'); ); } } }
  3. On the profile page you would call: $user = $gateway->findUserById(1);
  4. class CertifiedAds { function getAd($id = null) { return $id === null ? new AdList($this->db->fetchAll('SELECT * FROM ads')) : new AdList($this->db->fetchRow('SELECT * FROM ads WHERE id = ..')); } }
  5. I hope Database.php contains a Database class? If so, you write: class UserGateway { private $db = null; function __construct(Database $db) { $this->db = $db; } function getAllUsers() { return new UserList($this->db->fetchAll('SELECT * FROM users')); } function findUserById($id) { return new User($this->db->fetchRow('SELECT * FROM users WHERE id = ..')); } function findUsersByLastName($lastName) { return new UserList(..); } }
  6. By setMethod() is actually meant setter-methods aka mutator-methods. The reason you should not over-use them is because OO is about encapsulation if you create a set-method for each class variable, then it isn't encapsulated (shielded from the outside world) at all
  7. The website looks great only too bad it doesn't work with JS disabled.
  8. You used a global to pass an object while you should use the constructor or a setMethod() (just don't over-use the latter) class A { function sayHello($b) { echo 'Hello, ', get_class($b), '. How are you doing today?'; } } class B { function __construct(A $a) { $this->a = $a; } function greetings() { $this->a->sayHello($b); $this->sayHello($this->a); } function sayHello($a) { echo 'Fine, thanks for asking. And how are you doing on this sunny day ', get_class($a), '?'; } } $a = new A(); $b = new B($a); $b->greetings(); You could also pass $a on greetings() making it Lazy-Loaded
  9. Are you suggesting, he needs more private methods? Ever heard of a thing called Unit-Testing and the problem with private methods? Now you may think that you need MySQL for everything but if whatever you are building becomes a success you'll find your design is too inflexible. Let the DB handle DB specific stuff (connect(), disconnect(), prepare(), execute(), ..) and create Gateway classes that will hold an instance of your DB and let your application communicate with the Gateway this way you decouple your application from your DB, as you can modify the Gateway classes without having to touch your application code. class UserGateway { private $db = null; function getAllUsers() { return new UserList($this->db->fetchAll('SELECT * FROM users')); } function findUserById($id) { return new User($this->db->fetchRow('SELECT * FROM users WHERE id = ..')); } function findUsersByLastName($lastName) { return new UserList(..); } } Also a good idea is to not return array() or int or stuff like that but instead return domain-specific classes the reason being that you can't modify the internal structure of array() or int but you can with domain-related classes.
  10. Always stick to proper OO-design, if something doesn't work for some reason then you need to re-consider your design not hack it
  11. Remember that the software you write will not always be maintained by you. So whenever a new developer joins your team or someone takes over you maintenance work they won't have any documentation to refer to.
  12. The error message is self explanatory the log-in details are wrong! Show us a screen of you being logged into PHPMyAdmin this will give us more information to what the problem is.
  13. 100.000.000 I doubt you would want to insert all these records yourself, so a better approach would be: 1) define the boundaries of the game (can be any high number) 2) players are assigned a x- and y-coordinate (which needs to be unique and within the game boundaries for obvious reasons) 3) build the map: load any player-specific tiles for those present in the area and randomly generate tiles for all non-occupied tiles
  14. This script is incomplete and needs tweaking (hostname, user, pass, error descriptions, ..). It uses the SELECT .. INTO OUTFILE and LOAD DATA INFILE SQL commands. try { // database server #1 $db1 = db('db', 'pass', 'user', 'server1'); $outfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'outfile.txt'; if(!mysql_query("SELECT * FROM table INTO OUTFILE '$outfile'")) throw new Exception(''); if(!file_exists($outfile) || filesize($outfile) == 0) throw new Exception(''); // database server #2 $db2 = db('db', 'pass', 'user', 'server2'); if(!mysql_query("LOAD DATA INFILE '$outfile' REPLACE INTO TABLE table")) throw new Exception(''); } catch(Exception $e) { echo $e->getMessage(); } function db($database, $pass, $user = 'root', $host = 'localhost') { $db = mysql_connect($host, $user, $pass); if($db == FALSE) throw new Exception(''); if(!mysql_select_db($database, $db)) throw new Exception(''); return $db; }
  15. SELECT * FROM Manufacturer WHERE ManufacturerId = (SELECT ManufacturerId FROM Product WHERE someUniqueColumn = 'someUniqueValue') Euh... Why need the sub-query? SELECT * FROM manufacturer JOIN product ON manufacturer.id = product.manufacturer_id WHERE product.some_unique_value = 'some_unique_value';
  16. Too far fetched to me. If he wants to print it out then simply show him a [print] button that reads out your records and put's it in a table (repeat headers every x records) and implement a print-media stylesheet. <link href="print.css" type="text/css" media="print" rel="stylesheet">
  17. Completely unnecessary: define('OBJECT','mysql_fetch_object'); define('ASSOC','mysql_fetch_assoc'); And can be re-written to: class Database { const FETCH_OBJECT = 'object'; const FETCH_ASSOC = 'assoc'; const FETCH_BOTH = 'both'; } Also completely useless is: define('DB_HOST','localhost'); define('DB_DB','mysite'); define('DB_USER','root'); define('DB_PASS',''); Not everyone uses constants, some like to use configuration files, environment variables, .. Your class although named db is mysql-specific, if I would want to shift from mysql to sqlite for example I would have to re-write most (if not all) of your methods. Plus in some cases (like my last project) I had to use mysql as my main database which was kept in sync with a program that ran the back-office and SQLite as a buffer to hold the most frequent executed queries to lower the load on the main mysql database.
  18. reg.php also needs session_start() at the top.
  19. Add a custom route $router->addRoute( 'someroute', new Zend_Controller_Router_Route('lang/:lang/content/:content', array('controller' => 'index', 'action' => 'index')) ); $this->url(array('lang' => 'EN', 'content' => 5), 'someroute');
  20. We had actually could of have helped you better if you instead of Test had put up a real example of your code. I would create a Façade (or a Builder if you are actually building something in steps) if those methods needs to be called in sequence. Because you are familiar with those functions and how they are called, you can't assume someone else will if they join your project.
  21. if(logged_in() && $post['auhtor_id'] == $user['id'])
  22. Prolly, I had a chat 2 or 3 weeks back where I suggested HTML5 as a solution to his problem and replied "HTML5?! No one uses that anymore." Yup some really believe XHTML is the future
  23. class SomeClass { function __call($method, $args) { if(method_exists($this, $method)) return $this->$method(); return NULL; } function call($method, $args) { return $this->__call($method, $args); }} $class = new SomeClass();$output = $class->call($_GET['action']);
×
×
  • 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.