7

I trying to learn OOP and I've made this class

class boo{

  function boo(&another_class, $some_normal_variable){
    $some_normal_variable = $another_class->do_something(); 
  }

  function do_stuff(){
    // how can I access '$another_class' and '$some_normal_variable' here?
    return $another_class->get($some_normal_variable);
  }

}

and I call this somewhere inside the another_class class like

$bla = new boo($bla, $foo);
echo $bla->do_stuff();

But I don't know how to access $bla, $foo inside the do_stuff function

2

4 Answers 4

12
<?php
class Boo
{

    private $bar;

    public function setBar( $value )
    {
        $this->bar = $value;
    }

    public function getValue()
    {
        return $this->bar;
    }

}

$x = new Boo();
$x->setBar( 15 );
print 'Value of bar: ' . $x->getValue() .  PHP_EOL;

Please don't pass by reference in PHP 5, there is no need for it and I've read it's actually slower.

I declared the variable in the class, though you don't have to do that.

Sign up to request clarification or add additional context in comments.

3 Comments

Like @Logan said - Bottom line is if you want to save some information - then you need to save it in the local class.
I wouldn't make the blanket statement "don't pass by reference in PHP5." Maybe not for composition, but what if you wanted to modify an array in place? Passing by reference has its advantages.
you should probably say "there is rarely a need for it". In most cases it is unnecessary.
10

Ok, first off, use the newer style constructor __construct instead of a method with the class name.

class boo{

    public function __construct($another_class, $some_normal_variable){

Second, to answer your specific question, you need to use member variables/properties:

class boo {
    protected $another_class = null;
    protected $some_normal_variable = null;

    public function __construct($another_class, $some_normal_variable){
        $this->another_class = $another_class;
        $this->some_normal_variable = $some_normal_variable;
    }

    function do_stuff(){
        return $this->another_class->get($this->some_normal_variable);
    }
}

Now, note that for member variables, inside of the class we reference them by prefixing them with $this->. That's because the property is bound to this instance of the class. That's what you're looking for...

Comments

3

In PHP, constructors and destructors are written with special names (__construct() and __destruct(), respectively). Access instance variables using $this->. Here's a rewrite of your class to use this:

class boo{

  function __construct(&another_class, $some_normal_variable){
    $this->another_class = $another_class;
    $this->some_normal_variable = $another_class->do_something(); 
  }

  function do_stuff(){
    // how can I access '$another_class' and '$some_normal_variable' here?
    return $this->another_class->get($this->some_normal_variable);
  }

}

4 Comments

"For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics." - PHP Constructors and Destructors. Using the new syntax is still a good idea, as backwards compatibility may be dropped in the future.
@zzzzBov right, I would never tell someone to start using something that was deprecated. IMO this style is more clear.
I was just trying to add clarity as to why @Alexandra's code works (for the constructor that is).
What people still use PHP < 5...that would be crazy!
1

You need to capture the values in the class using $this:

$this->foo = $some_normal_variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.