MasterACE14 Posted July 25, 2011 Share Posted July 25, 2011 Good day, I have an Abstract 'Database' class and a 'MySQL' child class. Currently I'm passing the host, username, password and database name through the MySQL class constructor. Is there a better way of passing the login details to the 'MySQL' child class, whether it is through the Abstract class? or another way? Thanks, Ace Quote Link to comment https://forums.phpfreaks.com/topic/242707-database-abstract-class-mysql-child-class-passing-login-details/ Share on other sites More sharing options...
trq Posted July 25, 2011 Share Posted July 25, 2011 There are lot's of ways of doing this but it is generally accepted as common practice to pass such arguments to the __construct. Quote Link to comment https://forums.phpfreaks.com/topic/242707-database-abstract-class-mysql-child-class-passing-login-details/#findComment-1246644 Share on other sites More sharing options...
MasterACE14 Posted July 25, 2011 Author Share Posted July 25, 2011 expanding on your example from yesterday thorpe, if I have the __construct() in Class 'A', when Class A is instantiated and passed to the User class, I need to pass $host, $user, $pass, $dbname everytime Class A is passed to a class? or is there a better/different way of doing this? Abstract class Foo { public function bar() {} } class User { private $foo; public function __construct(Foo $foo) { $this->foo = $foo; } public function something() { echo $this->foo->bar(); } } class A extends Foo { private $host, $user, $pass, $dbname; public function __construct($host,$user,$pass,$dbname) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->dbname = $dbname; } public function bar() { return 'this is A::bar()'; } } } $user1 = new User(new A('localhost','root','pass','db')); Quote Link to comment https://forums.phpfreaks.com/topic/242707-database-abstract-class-mysql-child-class-passing-login-details/#findComment-1246652 Share on other sites More sharing options...
trq Posted July 25, 2011 Share Posted July 25, 2011 You can simply an instance and then pass it to User (which is what you are already doing anyway). This way it's easier if you need to use A in multiple objects. $a = new A('localhost','root','pass','db'); $user1 = new User($a); This is generally the simplest option. The above example is dependency injects at it's simplest. There are however more flexible (and more complex) patterns you can use. Google 'PHP Dependency Injection' and you should find some tutorials. There is also a decent library floating around these days from the guys who wrote Symfony. It's well worth taking a look at even if you don't end up using it. Quote Link to comment https://forums.phpfreaks.com/topic/242707-database-abstract-class-mysql-child-class-passing-login-details/#findComment-1246658 Share on other sites More sharing options...
MasterACE14 Posted July 25, 2011 Author Share Posted July 25, 2011 You can simply an instance and then pass it to User ah I missed the obvious. I will definitely check out dependency injection. Thanks again! -Ace Quote Link to comment https://forums.phpfreaks.com/topic/242707-database-abstract-class-mysql-child-class-passing-login-details/#findComment-1246666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.