943,177 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 95
  • PHP RSS
Sep 3rd, 2010
0

Object within object without inheritance

Expand Post »
Hi everyone hope you can help me with this issue, I'm trying to make a login system that is object oriented so I have the following classes:
  • MySQLDB: Peforms all database operations, adding/removing members, checking passwords and usernames, also performing querys.
  • Session: Performs Session actions(log in/out, keeping track of guests )
The Session class needs the MySQLDB class but DOES NOT inherit it, so after I define the MySQLDB class I also instance it as a global variable ($database), then I define the Session class, I know the basics of OOP and stuff, but the part which confuses me is that if the database object should be instanced within the Session class definition (as a member of Session) or outside with global scope (as an independent object), I think it should be instanced within the session class, but the login example which I'm being based initializes the database outside of session and redeclares it as global each time a method of session requires it, this is the original script I'm using as a guide, a bit old but very clear (except for that hehe) Login System also here's my code it's a simplified version to better illustrate my point.

database.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. class MySQLDB
  3. {
  4. private $link;
  5. private $etcetera;
  6. }
  7. //Instanced outside of Session
  8. global $database;
  9. $database = new MySQLDB();
  10. ?>
session.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. include "database.php";
  3. class session
  4. {
  5. /*OR instance the database object here so it can be accessed from the diffrent functions*/
  6. private $database= new MySQLDB();
  7.  
  8. function login($member,$password)
  9. {
  10. global $database; //or here?, every time a function needs it
  11. }
  12. }
  13. ?>
Regards,Triztian
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Sep 4th, 2010
0
Re: Object within object without inheritance
Personally I think the best implementation is to use the Singleton design pattern and not the global keyword in this instance. So your code for MYSQLDB class would be:

PHP Syntax (Toggle Plain Text)
  1. class MYSQLDB
  2. {
  3. private static instance = null;
  4. // private constructor and clone methods
  5. private __construct()
  6. {
  7. // initialise any variables here
  8. }
  9. private __clone()
  10. {
  11. }
  12. public static function getInstance()
  13. {
  14. // this code ensures there is only ever at most one instance of this class
  15. if (instance == null)
  16. instance = new MYSQLDB();
  17. return instance;
  18. }
  19. public function validateLogin($member, $password)
  20. {
  21. // check the member/password details in the database...
  22. }
  23. }

Then in your session class you call the getInstance method to initialise your database accessor like so:

PHP Syntax (Toggle Plain Text)
  1. class session
  2. {
  3. private $database;
  4. public __constuct()
  5. {
  6. $this->database = MYSQLDB::getInstance();
  7. }
  8. function login($member, $password)
  9. {
  10. // now you can use your database variable like so:
  11. $this->database->validateLogin($member, $password);
  12. }
  13. }

I believe this method is cleaner than the global approach, but I'll leave it up to you to decide what's best for your application.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Sep 4th, 2010
0

Clear some doubts..

Click to Expand / Collapse  Quote originally posted by darkagn ...
Personally I think the best implementation is to use the Singleton design pattern and not the global keyword in this instance. So your code for MYSQLDB class would be:

PHP Syntax (Toggle Plain Text)
  1. class MYSQLDB
  2. {
  3. private static instance = null;
  4. // private constructor and clone methods
  5. private __construct()
  6. {
  7. // initialise any variables here
  8. }
  9. private __clone()
  10. {
  11. }
  12. public static function getInstance()
  13. {
  14. // this code ensures there is only ever at most one instance of this class
  15. if (instance == null)
  16. instance = new MYSQLDB();
  17. return instance;
  18. }
  19. public function validateLogin($member, $password)
  20. {
  21. // check the member/password details in the database...
  22. }
  23. }

Then in your session class you call the getInstance method to initialise your database accessor like so:

PHP Syntax (Toggle Plain Text)
  1. class session
  2. {
  3. private $database;
  4. public __constuct()
  5. {
  6. $this->database = MYSQLDB::getInstance();
  7. }
  8. function login($member, $password)
  9. {
  10. // now you can use your database variable like so:
  11. $this->database->validateLogin($member, $password);
  12. }
  13. }

I believe this method is cleaner than the global approach, but I'll leave it up to you to decide what's best for your application.
Hi, thank you for your reply.
Even though I understand the concept of the singleton Design pattern and I do think that that's the better approach would you mind clarifying some aspects of the code, for example.
  • What's the use of "public static function"?
  • How does it ensure that only on instance of the object is created, does it destroy other objects of the same class if they are created?
  • Why "static instance"?
Hope you don't mind it's a bit confusing.
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Sep 5th, 2010
0
Re: Object within object without inheritance
public static function: this declaration indicates that the function is public so can be called from outside the class (for example, you can call the getInstance function from the session class. The static part of the declaration indicates that it is not called directly by a MYSQLDB instance variable. Instead, you call it like so:
PHP Syntax (Toggle Plain Text)
  1. $database = MYSQLDB::getInstance();

Because the constructor and clone functions have been made private, they can't be called from outside the class. This means that the only way to get an object of type MYSQLDB is by calling the getInstance function. This function creates an instance if it hasn't yet been created but if it has it is simply returned. Therefore there can only ever be one instance of the object.

Sorry, there are a couple of errors in my code for the MYSQLDB class, I was getting confused with C# syntax. The corrections are:
PHP Syntax (Toggle Plain Text)
  1. // this instantiates a class variable called $instance to a null value
  2. private static $instance = null;
  3. //...
  4. public static function getInstance()
  5. {
  6. // what we are doing here is checking whether the instance has been created
  7. // if it hasn't it is created
  8. if (MYSQLDB::instance == null)
  9. MYSQLDB::instance = new MYSQLDB();
  10. // now we return it
  11. return MYSQLDB::instance;
  12. }

I hope this has helped, please let us know if you are still unsure of what I have described.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Sep 5th, 2010
0
Re: Object within object without inheritance
Darkagn, do I miss something here.
I think you forgot else clause. I stand to be corrected though.
I think if you call the function twice it wont return anything as the MYSQLDB::instance will not be null. Also in C++ you must Delete the instance once used but I think PHP does that for you.

PHP Syntax (Toggle Plain Text)
  1. public static function getInstance()
  2. {
  3. // this code ensures there is only ever at most one instance of this class
  4. if (instance == null){
  5. instance = new MYSQLDB();
  6. return instance;
  7. }
  8. else{
  9. return instance;
  10. }
  11. }
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Sep 6th, 2010
0
Re: Object within object without inheritance
If the $instance variable is null, it is created then returned. If it is not null, it is simply returned without being re-created. Your example is equivalent however. Note that my code does not have curly braces for the if-statement, so only the first line after the condition is skipped if the condition is false.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Sep 6th, 2010
0
Re: Object within object without inheritance
Hi, thanks a lot for your help, cleared my doubts and learned something new, keep up the good work darkagn.

Regards, Triztian.
Last edited by Triztian; Sep 6th, 2010 at 3:11 am.
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Sep 10th, 2010
0
Re: Object within object without inheritance
Click to Expand / Collapse  Quote originally posted by darkagn ...
If the $instance variable is null, it is created then returned. If it is not null, it is simply returned without being re-created. Your example is equivalent however. Note that my code does not have curly braces for the if-statement, so only the first line after the condition is skipped if the condition is false.
I get it,
it is little tricky until you consider the "no brace" thing and static behaviour
Thanks for explanations
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: This forum
Next Thread in PHP Forum Timeline: how to open 2nd file on Eclipse in new tab





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC