This content originally appeared on DEV Community and was authored by shoaiyb sysa
Yes it's possible to use $this outside class with php, But how?
It can be done by including the file that contains $this
inside the class, Just like below.
Example:
class.php
<?php
class MyClass {
private $say = 'I didn\'t say anything!';
public function say(string $text) {
$this->say = $text;
}
public function hear() {
require __DIR__ . '/test.php';
echo $this->say;
}
}
$MyClass = new MyClass();
$MyClass->hear();
?>
test.php
<?php
$this->say('I Said Using $this Outside Class Is Possible!');
?>
This content originally appeared on DEV Community and was authored by shoaiyb sysa
shoaiyb sysa | Sciencx (2021-11-21T10:08:53+00:00) Can $this be Used Outside Class (PHP). Retrieved from https://www.scien.cx/2021/11/21/can-this-be-used-outside-class-php/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.