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.

Recommended Answers

All 11 Replies

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.

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.

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

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.

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.

Thanks i will have to read up on that.

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

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.

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();
?>

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.