943,755 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 4129
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 25th, 2007
0

Object Oriented PHP Question

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 25th, 2007
0

Re: Object Oriented PHP Question

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.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 25th, 2007
0

Re: Object Oriented PHP Question

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.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 25th, 2007
0

Re: Object Oriented PHP Question

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
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 25th, 2007
0

Re: Object Oriented PHP Question

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.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 25th, 2007
0

Re: Object Oriented PHP Question

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
Click image for larger version

Name:	oop-entity-daniweb.jpg
Views:	348
Size:	9.3 KB
ID:	3806  
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 25th, 2007
0

Re: Object Oriented PHP Question

Thanks i will have to read up on that.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 25th, 2007
0

Re: Object Oriented PHP Question

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 26th, 2007
0

Re: Object Oriented PHP Question

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.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 26th, 2007
0

Re: Object Oriented PHP Question

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.

PHP Syntax (Toggle Plain Text)
  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. ?>
PHP Syntax (Toggle Plain Text)
  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. ?>
PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006

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: Site Not Working At New Host.
Next Thread in PHP Forum Timeline: PHP and XML





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


Follow us on Twitter


© 2011 DaniWeb® LLC