Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 21, 2021 10:08 am GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To