Page 3 of 4

Posted: Mon May 28, 2007 8:45 am
by superdezign
ole wrote:
I believe... C++ makes copies, but I think PHP creates references
PHP 4 and C++ both copy and are both ridiculous for it. Most languages reference including PHP 5.
Well, it's good and bad. Good because you don't risk harming/changing the original data, but bad because it's less efficient. I'm fine with it in C++ because you can easily turn any variable into a reference, though ampersands everywhere tends to look messy. Maybe they should have put it the other way around.

Posted: Mon May 28, 2007 8:50 am
by feyd
DIRECTORY_SEPARATOR is the proper thing to have. Stick with it.

Posted: Mon May 28, 2007 8:53 am
by superdezign

Code: Select all

str_replace('/', DIRECTORY_SEPARATOR, 'foo/bar/whatever.php');
Maybe? Hmm? Hmm? :-p

Posted: Mon May 28, 2007 9:13 am
by s.dot
ole wrote:Also have you considered following Zend or PEAR coding standards? See the first link in my sig.
Negative. I probably should, but I'm content with my own coding style. When I get good at this stuff, and writing something other people may want to contribute on or work with, then I'll learn the coding standards.

Although, I've read the standards before. I could do them but I just choose not to. For now I like to stick with underscores and all lowercase letters.

Posted: Mon May 28, 2007 2:58 pm
by Christopher
A couple of comments.

1. As for isError() and getError() they would return true/false or a string respectively. They are used to check if an error occurred by saving the error state after the query or fetch.

2. I prefer to always return an object from the query() method. It makes it consistent to deal with rather than it being either an object or false. Especially if you pass it, then the dependency is clean/clear.

3. I would recommend that you change the naming now. It is not a good practice become too familiar with you own non-standard naming. I consider being open and flexible to the advance of standards to be a sign of a mature programmer.

Posted: Mon May 28, 2007 3:04 pm
by s.dot
IMO, the standard doSomethingHere is much uglier and harder to deal with thatn do_something_here, but eh, standards are standards, and who am I?

Posted: Mon May 28, 2007 3:13 pm
by superdezign
scottayy wrote:IMO, the standard doSomethingHere is much uglier and harder to deal with thatn do_something_here, but eh, standards are standards, and who am I?
I think the do_something_here is the style they use for you to be able to differentiate between built-in functions and your own. (Correct me if I'm wrong.)

But yeah, I don't like have the first letter of a function lowercased... The standards I code by are most derived from C++, however typing the dollar sign made me stop using prefixes... made me press shift too often. :-p

Posted: Mon May 28, 2007 3:30 pm
by s.dot
So, how's this conform to the standards (besides documentation blocks)

Code: Select all

<?php

class db_connect
{
    private $_host;
    private $_username;
    private $_password;
    private $_linkId;
    
    public function __construct($host, $username, $password)
    {
        $this->_host = $host;
        $this->_username = $username;
        $this->_password = $password;
    }
    
    public function connect()
    {
        $this->_linkId = mysql_connect($this->_host, $this->_username, $this->_password);
    }
    
    public function useDb($db)
    {
        mysql_select_db($db, $this->_linkId);
    }
    
    public function escape($var)
    {
        return mysql_real_escape_string($var, $this->_linkId);
    }
    
    public function query($sql)
    {
        if (!$this->_linkId) {
            $this->connect();
        }
        
        if (is_resource($result = mysql_query($sql, $this->_linkId))) {
            require_once getcwd() . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'db_result.class.php';
            return new db_result($result);
        }
        
        return false;
    }
}

Code: Select all

<?php

class db_result
{
    private $_result;
    
    public function __construct($result)
    {
        $this->_result = $result;
    }
    
    public function numRows()
    {
        return mysql_num_rows($this->_result);
    }
    
    public function fetchRow($fetch_type=MYSQL_ASSOC)
    {
        return mysql_fetch_array($this->_result, $fetch_type);
    }
}

Posted: Tue May 29, 2007 1:59 am
by vigge89
You asked for articles on some of the stuff in OO earlier, and since I'm in a rush I'll just post them if you're still interested:
http://hudzilla.org/phpwiki/index.php?t ... rogramming
Something I read yesterday (just started with OOP), I found the article quite good.

Posted: Tue May 29, 2007 11:29 am
by s.dot
Thank you for that link! It seems very in depth. I read a few pages and added it to my favorites.

Posted: Tue May 29, 2007 12:03 pm
by John Cartwright
I don't know if everyone will agree, but the query method

Code: Select all

public function query($sql)
    {
        if (!$this->_linkId) {
            $this->connect();
        }
       
        if (is_resource($result = mysql_query($sql, $this->_linkId))) {
            require_once getcwd() . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'db_result.class.php';
            return new db_result($result);
        }
       
        return false;
    }
Should have no business requiring the file itself. Either that should be re factored into it's own method of the result class is simply lazy loaded. Probably the latter.

The next step would be supporting result operations for non-select's. For instance, currently numRows() and fetchRow() have no business for in update/insert operation. Polymorphism could be applied to retrieve the correct result class depending on the query itself.

Posted: Tue Jul 24, 2007 2:37 pm
by thinsoldier
why does one class handle both connecting and querying and another class for the results?

Code: Select all

require_once getcwd().DIRECTORY_SEPARATOR.'class'.DIRECTORY_SEPARATOR.'db_result.class.php';
                        return new db_result($this->link_id, $result); 

see: http://bs.php.net/autoload

By the second page I definitely don't understand the need for the result to by handled by a separate class. Anyone able to explain why to me?[/syntax]

Posted: Tue Jul 24, 2007 4:49 pm
by feyd
They're separate things. They have separate verbs and data.

Posted: Wed Jul 25, 2007 12:21 pm
by thinsoldier
feyd wrote:They're separate things. They have separate verbs and data.
could i get a better explanation?

Posted: Wed Jul 25, 2007 12:23 pm
by John Cartwright
thinsoldier wrote:
feyd wrote:They're separate things. They have separate verbs and data.
could i get a better explanation?
They have separate responsibilities, therefore should be represented as different objects. Yes, you certainly could put them into a single object, but thats not good OOP.