Object Oriented PHP Question

Reply

Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Object Oriented PHP Question

 
0
  #1
Jul 25th, 2007
I've just started looking at OO PHP and was wondering how you would access an object in a script other than the one that instantiated it.

For example:

  1. <?php // file: animal.php
  2.  
  3. Class Animal
  4. {
  5. $name;
  6. $noise;
  7.  
  8. function _construct($nm, $no)
  9. {
  10. $this->name = $nm;
  11. $this->noise = $no;
  12. }
  13.  
  14. function noise()
  15. {
  16. return $this->$noise;
  17. }
  18. }
  19.  
  20. $dog = new Animal("dog", "Woof!");
  21.  
  22. ?>

How could i access the $dog object in a different script?

Any help would be much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #2
Jul 25th, 2007
You wouldnt really create the object of Animal in its own class unless you really need to.

After creating the class and all its methods.

You just need to do.

  1. include_once("dir/clsAnimal.php");
  2.  
  3. $dog = new Animal("dog", "Woof!");
  4. $noise = $dog->noise();
  5. echo $noise;
  6.  
  7. NOTE: Within your class your instance variables '$noise' and '$name' you should declare them specifying the type of variable it is.
  8.  
  9. Being 'public' 'protected' or 'private'.
  10. eg:
  11. public $noise;
  12. public $name;


code is fine, but you generally include the class then create an object out of it.

Cheers.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #3
Jul 25th, 2007
Thanks for your reply.

I just wrote that example quickly. I fully appreciate most OO concepts like access control etc. What i really want to do is load a class with data from a database and be able to access it throughout an entire web application. I was just wondering if this was possible some how.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #4
Jul 25th, 2007
Do you want to create the class using DB information?

Or get DB information when creating the class ?

Just clarifying before i answer your question
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #5
Jul 25th, 2007
Basically when a user successfully logs in i want to instantiate an object and load it with the users details. The class would have functions to manipulate the date etc. I would like to be able to access this object through all scripts on the site if its possible.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #6
Jul 25th, 2007
What you want to do, is create a global space for your site,
and depending on which action your performing, include the appropriate class and content pages.

What i do is make a module (eg: for handling all User operations).
which then includes the content.

I have attached an image to help explain how to do things in OOP.
It's merley an example there are 101 ways to skin a cat. just your preference really.

Cheers.
Attached Thumbnails
oop-entity-daniweb.jpg  
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #7
Jul 25th, 2007
Thanks i will have to read up on that.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Object Oriented PHP Question

 
0
  #8
Jul 25th, 2007
You can store your object in $_SESSION[] if you need to maintain it throughout many page views. Keep in mind you can't have session_autostart turned on if you wish to include user objects in the SESSION global array, since it needs to load the class definition before the session is started.
See here for more info:
http://www.php.net/manual/en/ref.session.php
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #9
Jul 26th, 2007
Thanks, i tried this but had problems accessing the object. But i'll try again. I assume that session auto start is a PHP configuration to stop you have to type session_start.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Object Oriented PHP Question

 
0
  #10
Jul 26th, 2007
Ok, i can't seem to do it. Would you mind telling me what i'm doing wrong. I get an error in the third script.

  1. <?php //animal.inc
  2.  
  3. class Animal
  4. {
  5. private $noise;
  6.  
  7. function __construct($param)
  8. {
  9. $this->noise = $param;
  10. }
  11.  
  12. function makeNoise()
  13. {
  14. return $this->noise;
  15. }
  16. }
  17.  
  18. ?>
  1. <?php //instantiate.php
  2.  
  3. require('animal.inc');
  4.  
  5. session_start();
  6.  
  7. $dog = new Animal("Woof!");
  8.  
  9. $_SESSION[obj] = $dog;
  10.  
  11. ?>
  1. <?php //call.php
  2.  
  3. require('animal.inc');
  4.  
  5. session_start();
  6.  
  7. echo " a dog goes woof";
  8.  
  9. $dog = $_SESSION[obj];
  10.  
  11. //error on next line
  12. //$noise = $dog->makeNoise();
  13. ?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC