| | |
Object Oriented PHP Question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2006
Posts: 162
Reputation:
Solved Threads: 14
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:
How could i access the $dog object in a different script?
Any help would be much appreciated.
For example:
PHP Syntax (Toggle Plain Text)
<?php // file: animal.php Class Animal { $name; $noise; function _construct($nm, $no) { $this->name = $nm; $this->noise = $no; } function noise() { return $this->$noise; } } $dog = new Animal("dog", "Woof!"); ?>
How could i access the $dog object in a different script?
Any help would be much appreciated.
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.
code is fine, but you generally include the class then create an object out of it.
Cheers.
After creating the class and all its methods.
You just need to do.
PHP Syntax (Toggle Plain Text)
include_once("dir/clsAnimal.php"); $dog = new Animal("dog", "Woof!"); $noise = $dog->noise(); echo $noise; NOTE: Within your class your instance variables '$noise' and '$name' you should declare them specifying the type of variable it is. Being 'public' 'protected' or 'private'. eg: public $noise; 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
•
•
Join Date: Sep 2006
Posts: 162
Reputation:
Solved Threads: 14
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.
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.
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.
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.
GardCMS :: Open Source CMS :: Gardcms.org
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
See here for more info:
http://www.php.net/manual/en/ref.session.php
•
•
Join Date: Sep 2006
Posts: 162
Reputation:
Solved Threads: 14
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)
<?php //animal.inc class Animal { private $noise; function __construct($param) { $this->noise = $param; } function makeNoise() { return $this->noise; } } ?>
PHP Syntax (Toggle Plain Text)
<?php //instantiate.php require('animal.inc'); session_start(); $dog = new Animal("Woof!"); $_SESSION[obj] = $dog; ?>
PHP Syntax (Toggle Plain Text)
<?php //call.php require('animal.inc'); session_start(); echo " a dog goes woof"; $dog = $_SESSION[obj]; //error on next line //$noise = $dog->makeNoise(); ?>
![]() |
Similar Threads
- Oo Php (PHP)
- Object Oriented Design (PHP)
Other Threads in the PHP Forum
- Previous Thread: Site Not Working At New Host.
- Next Thread: PHP and XML
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube






