max0005
max0005

Reputation: 210

Access class property from outside of class

Suppose I had the following class:

class MyClass {
    public function Talk() {
        $Say = "Something";
        return $Say;
    }
}

I then started an instance of the class:

$Inst = new MyClass();

How can I now call $Say outside MyClass hence, for example, echo it on the document? For example, having something like:

$Said = "He" . $Say

Upvotes: 4

Views: 9933

Answers (6)

Vitalii Lebediev
Vitalii Lebediev

Reputation: 662

The best practices of oop is NEVER have public properties in your class. The best way to manipulate properties of your class is to have separate methods that will return the value of the properties and set the value of the properties. So

class MyClass {
    private $say;

    // common setter, invoked when we trying to set properties as they are public
    public function __set($name, $value) {
        $methodname = 'set' . ucfirst(strtolower($name));
        if (method_exists($this, $methodname)) {
            $this->$methodname($value);
        } else {
            throw new Exception ('There is no "' . $name . '" property in "' . __CLASS__ . '" class');
        }
    }

    // common getter, invoked when we trying to get properties as they are public
    public function __get($name) {
        $methodname = 'get' . ucfirst(strtolower($name));
        if (method_exists($this, $methodname)) {
            return $this->$methodname();
        } else {
            throw new Exception ('There is no "' . $name . '" property in "' . __CLASS__ . '" class');
        }
    }

    // setter for out private property $say, invoked by common setter when colling $a->say = "something";
    public function setSay($value) {
        $this->say = $value;
    }

    // getter for out private property $say, invoked by common getter when colling $a->say;
    public function getSay() {
        return $this->say;
    }

    public function Talk($monologue) {
        $this->say = (!empty($monologue))?$this->say:$monologue;
        return $this->say;
    }

}

So now you can access your private properties as they are public, and do all neccessary checks to not get bad values be stored in them. Like that:

$a = new MyClass;
$a->say = "Hello my friends !";
$a->Talk();
$a->Talk("Good bye !");
echo $a->say;

Or like that:

$a = new MyClass;
$a->setSay("Hello my friends !");
$a->Talk();
$a->Talk("Good bye !");
echo $a->getSay();

For more security, you can make setSay and getSay methods private, but then the second piece of code won't work.

Upvotes: 1

IOInterrupt
IOInterrupt

Reputation: 539

You need to make the $Say an instant variable of the MyClass class.

class MyClass {
    public $Say
    public function Talk() {
        $this->Say = "Something";
        return $this->Say;
    }
}

You could then access the instance variable from outside the class via $Inst->Say

Additionally it is better practice to encapsulate your class instance variables and use a "getter" method to retrieve the values.

class MyClass {
    private $Say
    public function Talk() {
        $this->Say = "Something";
        return $this->Say;
    }
    public getSay() {
        return $this->Say;
    }
}

$Inst = new MyClass();
echo $Inst->getSay();

Upvotes: 1

user827080
user827080

Reputation:

You'd need to declare that var before your functions, eg

class MyClass {
  public $say;
  function Talk() {
    $this->say = "something";
  }
}

and then

$Said = "He ".$Inst->$say;

Upvotes: 0

simshaun
simshaun

Reputation: 21466

I strongly recommend you read through http://php.net/manual/en/language.oop5.php. It will teach you the fundamentals of OOP in PHP.


In your example, $Say is just another variable declared within Talk()'s scope. It is not a class property.

To make it a class property:

class MyClass {
    public $say = 'Something';

    public function Talk() {
        return $this->say;
    }
}

$inst = new MyClass();
$said = 'He ' . $inst->say;

That defeats the purpose of Talk() however.
The last line ought to be $said = 'He '. $inst->Talk();

Upvotes: 8

jabbany
jabbany

Reputation: 529

You can use

$said = "He" . $Inst->Talk();

in this case, or you can class

class MyClass {
var $say;
public function Talk() {
    $say = "Something";
    $this->say = $say;
    return $say;
}
}

and call

$said = "He" . $Inst->say;

Upvotes: -1

Jeff Lambert
Jeff Lambert

Reputation: 24661

$say is not a class property. If it was, you would define your class like this:

class MyClass {
    public $say;      
}

It is instead a local variable of the function Talk(). If you want to access it the way that you have the class defined, you would do:

$instance = new MyClass();
$instance->Talk();

Upvotes: 1

Related Questions