Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Server Development
Habbo Retros
Habbo Development
Flashback PHP - A Habbo CMS written in vanilla PHP
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Markshall" data-source="post: 465279" data-attributes="member: 1872"><p>Yep, there’s nothing wrong with doing an if/else statement in the manner you’re proposing to do it. That’s what they’re there for.</p><p></p><p>What I was referring to in my original post is how to structure the return statement of your userLogin method.</p><p></p><p>As for passing your PDO instance as a parameter to another object, that’s fine too. It’s a technique called dependency injection and is a common thing to do. What may be a good idea is to create a parameter which accepts an array of dependencies in case your app grows and you don’t end up having a method accepting 5 parameters all for different instances of other objects, instead it receives 1 parameter which is just an array of this different instances.</p><p></p><p>For example, instead of:</p><p>[code=php]class User {</p><p> public function __construct($db, $anotherClass, $anotherClassAgain) {</p><p> $this->db = $db;</p><p> $this->anotherClass = $anotherClass;</p><p> $this->anotherClassAgain = $anotherClassAgain;</p><p> }</p><p>}</p><p>$User = new User($db, $anotherClass, $anotherClassAgain);</p><p>[/code]</p><p>You could just do:</p><p>[code=php]class User {</p><p> public function __construct($deps) {</p><p> foreach ($deps as $name => $instance) {</p><p> $this->$name = $instance;</p><p> }</p><p> }</p><p>}</p><p>$User = new User([</p><p> 'db' => $db,</p><p> 'anotherClass' => $anotherClass,</p><p> 'anotherClassAgain' => $anotherClassAgain</p><p>]);[/code]</p><p></p><p>I hope what I’m saying is making sense lol. I’m typing this on my iPhone so it’s hard writing these code examples.</p></blockquote><p></p>
[QUOTE="Markshall, post: 465279, member: 1872"] Yep, there’s nothing wrong with doing an if/else statement in the manner you’re proposing to do it. That’s what they’re there for. What I was referring to in my original post is how to structure the return statement of your userLogin method. As for passing your PDO instance as a parameter to another object, that’s fine too. It’s a technique called dependency injection and is a common thing to do. What may be a good idea is to create a parameter which accepts an array of dependencies in case your app grows and you don’t end up having a method accepting 5 parameters all for different instances of other objects, instead it receives 1 parameter which is just an array of this different instances. For example, instead of: [code=php]class User { public function __construct($db, $anotherClass, $anotherClassAgain) { $this->db = $db; $this->anotherClass = $anotherClass; $this->anotherClassAgain = $anotherClassAgain; } } $User = new User($db, $anotherClass, $anotherClassAgain); [/code] You could just do: [code=php]class User { public function __construct($deps) { foreach ($deps as $name => $instance) { $this->$name = $instance; } } } $User = new User([ 'db' => $db, 'anotherClass' => $anotherClass, 'anotherClassAgain' => $anotherClassAgain ]);[/code] I hope what I’m saying is making sense lol. I’m typing this on my iPhone so it’s hard writing these code examples. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Server Development
Habbo Retros
Habbo Development
Flashback PHP - A Habbo CMS written in vanilla PHP
Top