Sent from a handheld device

On 08-Dec-2011, at 11:14 PM, "Dominik Halvoník"
<dominik.halvo...@gmail.com> wrote:

> Hello,
>
> I would like to ask you for help. This days I am trying to build one of my
> applications. But I have problem which stopped me. I have folder whit php
> files like connect.php, delete.php etc. These files contains classes named
> the same as files. So in file connect.php is class Connect. These files are
> placed in folder named mysql and this folder is inside folder named db. In
> folder db is a php file named mysql.php, in this file I include classes
> from folder mysql, after include I declare class MySQL and in it I have
> method __construct(). In this method I create dynamic objects from included
> classes. And this is the problem that I can not solve, I have more then one
> of this files(mysql.php[whit class MySQL], oracle.php[whit class Oracle]
> etc.) and I need to include them to file called db.php that is in the main
> folder of my app. In db.php is an class called db, how can I add classes
> MySQL, Oracle etc. to class db? I try to use abstract class whit __set and
> __get methods but I also need to include class db to main class
> application. I am really sorry for my English, so please be indulgent. So I
> need to connect classes like this:
>

This is where common design patters comes handy.
Your problem can be solved by factory pattern.
Create a static method connect on db class. Then call it like
$db = DB::connect($db_type)

Here the db type variable contains MySQL or oracle. In connect static
method you implement the logic on how to connect that specific
database. In the connect method you can also take db credentials like
username and password.

Google "php design pattern" to know more.


> application->db->mysql->connect, but I can not use extends because in php
> you can have only one parent class. The reason why I am trying to do
> something like this is because I want to call methods like this:
> $test = new application();
> $test->db->connect();
>
> If it is mysql or othet database I set in config.php file. Can you help my
> please?
>
> Sincerely,
>
> Dominik Halvonik

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

Reply via email to