Re: Procedural to OOP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Mike Smith <mikeosmith@xxxxxxxxx>:
> I am rewriting a project, and have decided to use/learn OOP. I am
> using ADODB to connect to a MS SQL 2000 box. This code works, but I'd
> like to know if I'm following good form or if I'm totally missing the
> boat. I'd also like to include things like the
> dbhost/username/password, etc in a seperate config file would I...
>
> class foo{
>
> function bar(){
> include('my_config_file_here.php');// sets $dbhost etc...
>
> var $db_host = $dbhost;
> ...
> }
> }
>
> ...or is there another way.

I do the same thing... but only for areas that I consider static.
Otherwise, I pass the DSN in as a parameter to the object constructor --
and even then, I'm having it defined from a function that's in a file
outside my document root:

In Foo.class.php:

    require_once 'DB.php';
    class foo 
    {
        function foo($dsn)
        {
            // I use PEAR, so this is how I connect to a DB:
            $db =& DB::connect($dsn);
            $this->db =& $db;
        }
    }

In instance script:

    require_once 'Foo.class.php';
    require_once 'connection.php'; // this file is in the include path,
        // but not in the web tree

    $foo = new Foo(connectionFunc());

> Here is my class and my index.php which will present it. I looked at
> Smarty, but I think my head would explode trying to link
> ADODB-OOP-SMARTY. 

It's actually not difficult at all to connect ADODB/PEAR::DB to Smarty,
believe it or not. You pass resultsets directly to Smarty, and then use
Smarty's foreach or section constructs to loop over them. Once you do
that, I think you'll find your code becomes cleaner, and you have less
disconnect as you work in PHP (I often had disconnect when intermingling
HTML and PHP from trying to switch back and forth between the languages;
moving to a templating system was a huge productivity boost, as when you
work in a template, you use HTML; when you work in a script/class, you
use PHP).

-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux