954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Object Oriented PHP Question

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 // 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.

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

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.

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.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

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.

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

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

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

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.

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

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.

Attachments oop-entity-daniweb.jpg 9.26KB
dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Thanks i will have to read up on that.

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

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

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
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    //animal.inc

class Animal
{
    private $noise;
    
    function __construct($param)
    {
        $this->noise = $param;
    }
    
    function makeNoise()
    {
        return $this->noise;
    }
}

?>
<?php    //instantiate.php

    require('animal.inc');
    
    session_start();

    $dog = new Animal("Woof!");
    
    $_SESSION[obj] = $dog;
   
?>
<?php    //call.php

    require('animal.inc');
    
    session_start();

    echo " a dog goes woof";
    
    $dog = $_SESSION[obj];
    
    //error on next line
    //$noise = $dog->makeNoise();
?>
Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

I made the following change and it works fine

<?php    //instantiate.php

    require('animal.inc');
    
    session_start();

    $dog = new Animal("Woof!");
    
    $_SESSION['dog'] = $dog;    
   
?>

<?php    //call.php

    require('animal.inc');
    
    session_start();

    echo " a dog goes woof";
    
    $dog = $_SESSION['dog'];
    
    //error on next line
    $noise = $dog->makeNoise();
    echo "<BR><B>$noise</B>";
?>

The only change was to provide 'dog' as the key in the session array.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thanks, i think i was just being a bit stupid and calling the instatiate.php script thinking it was the other one.

Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 27
Solved Threads: 14
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You