Re: OOP problems
| From: | Alex Pojarsky | Date: | Thu, 15 Dec 2011 11:05:14 +0000 |
| Subject: | Re: OOP problems | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316025@lists.php.net to get a copy of this message | ||
I'm not sure I've understood you correctly, but you may try something
like the following primitive autoloader (I didn't debug it, it's just
an example):
class Base
{
protected $_path = '';
public function construct($base_path)
{
$this->_path = $base_path;
}
public function __get($name)
{
$requested_path = $this->_path . DIRECTORY_SEPARATOR . $name;
if (is_dir($requested_path))
{
return new Base($requested_path);
}
else if (is_file($requested_path . '.php'))
{
include ($requested_path . '.php');
$classname = ucfirst($name);
return new $clasname();
}
}
}
// Assuming you have Mysql class in /home/user/project/classes/db/mysql.php
// you may try
$base = new Base("/home/user/project/classes/");
$base->db->mysql->someFunctionOfMysqlClass();
2011/12/15 Dominik Halvoník <dominik.halvonik@gmail.com>:
> 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:
>
> 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.
>
> I need to achieve this schema( -> is something like ../ it means that it is
> one level up folder):
>
> connec.php(class Connect MySql)->
> select.php(class Select MySql) ->
> .... -> mysql.php(class MySQL include all classes, Connect...)->
> .... ->
> ... ->
> -> db.php(class db include all classes, MySQL, Oracle..)
> connec.php(class Connect Oracle)->
> select.php(class Select Oracle ) ->
> .... -> oracle .php(class Oracle include all classes, Connect...)->
> .... ->
> ... ->
>
> download.php(class Download)->
> unzip.php(class Unzip) ->
> .... -> files.php(class Files include all classes, Download...) ->
> file.php(class file include class Files)
> .... ->
> ... ->
>
> hash.php(class Hash)->
> capcha.php(class Capcha) ->
> .... -> secure.php(class Secure include all classes, Hash...) ->
> security.php(class security include class Secure)
> .... ->
> ... ->
> ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect..
>
> And in the end, in the same folder as db.php and security.php I will have
> file application.php which will contain class application and in its
> __construct() method I will make link classes db, security, file ect. ect..
> So I will just include file application.php make object from class
> application and then just do $object->db->connect()(of course if it will by
> MySql or other database will be stored in some config.php file).
>
> Thanks,
>
> Dominik