An Interest In:
Web News this Week
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
- March 25, 2024
November 21, 2021 10:08 am GMT
Original Link: https://dev.to/shoaiyb/can-this-be-used-outside-class-php-l29
Can $this be Used Outside Class (PHP)
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
<?phpclass 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!');?>
Original Link: https://dev.to/shoaiyb/can-this-be-used-outside-class-php-l29
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To