TheWebs
TheWebs

Reputation: 12923

Can this be done in PHP? possible global variable

So I have something like this:

class SomeClass{
    //do some stuff
}

$variable  = new SomeClass();

Now that in a php file called SomeClass.php for example, I want to in some other class called OtherClass.php do something like:

class OtherClass{
    function doSomething(){
        $variable->call_some_method();
    }
}

How would I achieve this? I have thought of global variables, but then I see frameworks like Zend doing stuff like: $this->view->what_i_want_to_pass_to_the_view = 'hello apple sauce';

So what's a good way to do, essentially what I want to do.

Note: Some people are assuming I want to do global variables, this is not true. I am trying to replicate how Zend does something like $this->view in a class to send something to the view and then in the view, calls $this->view to get what was sent. I assumed this was done through globals...

Upvotes: 0

Views: 100

Answers (4)

irishman
irishman

Reputation: 352

There are a few ways of doing it:

1) Extend SomeClass

class OtherClass extends SomeClass
{

 function doSomething(){
     $this->call_some_someclass_method();
 }

}

2) Pass the SomeClass instance into the OtherClass (not good coding practice)

$a = new SomeClass();
$b = new OtherClass();
$b->someInstance($a);

class OtherClass extends SomeClass
{
 function someInstance($someClass){
    $this->someClass = $someClass;
 }
 function doSomething(){
     $this->someClass->call_some_method();
 }

}

3) Create an instance inside OtherClass

class OtherClass extends SomeClass
    {
     function someInstance(){
        $this->someClass = new SomeClass;
     }
     function doSomething(){
         $this->someInstance();
         $this->someClass->call_some_method();
     }

    }

Upvotes: -1

njasm
njasm

Reputation: 431

I believe what you need is 'extends' i'll give you an example.

class Hello {

    public function world() {
        echo 'Hello World!';
    }

}

class someClass extends Hello {

    public function doSomething() {
        $this->world();
    }
}


$obj = new someClass();
$obj->doSomething();

Upvotes: 0

David Müller
David Müller

Reputation: 5351

This concept is called "Dependency injection" and is considered to be a best practice for doing even that.

class SomeClass{
    //do some stuff
}

$some  = new SomeClass();

class OtherClass{
    function doSomething(SomeClass $some){
        $some->call_some_method();
    }
}

$otherclass = new OtherClass();
$otherclass->doSomething($some);

Alternatively, you can just inject the dependency on constructing time:

class OtherClass{
    private $some = null;

    function __construct(SomeClass $some){
        $this->some = $some;
    }

    function doSomething(){
        $this->some->call_some_method();
    }
}

Depends on what you want to achieve and how often you need the dependency.

Upvotes: 4

Martin Bean
Martin Bean

Reputation: 39389

First, I’d like to congratulate you on finding a way to introduce global variables to Zend Framework as it breaks everything Zend Framework is about (OOP, MVC architecture etc).

Secondly, it’s bad practice. It’s possible, but bad too. If you find you’re struggling to do something with the language, then chances are there’s an easier way accomplish what you’re trying to do.

Although this isn’t an answer, I’d implore you to divulge more into your problem and why you’re wanting to use a class instance, globally-defined, in another class rather than an abstract example. Chances are, we can then help you become a better programmer by offering an alternative approach rather than instilling bad practices for you to go about building your websites and web applications.

Upvotes: 1

Related Questions