Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - passing variables to classes

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

like image 563
Alex Avatar asked Feb 02 '11 17:02

Alex


People also ask

How do you declare a class variable in PHP?

The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.

How can you pass a variable by reference in PHP?

Pass by reference: When variables are passed by reference, use & (ampersand) symbol need to be added before variable argument. For example: function( &$x ). Scope of both global and function variable becomes global as both variables are defined by same reference.


3 Answers

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

like image 171
Logan Bailey Avatar answered Oct 14 '22 07:10

Logan Bailey


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

like image 44
ircmaxell Avatar answered Oct 14 '22 05:10

ircmaxell


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);
  }

}
like image 33
Rafe Kettler Avatar answered Oct 14 '22 05:10

Rafe Kettler