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

<?php
class MyClass {
private $say = ‘I didn\’t say anything!’;
p…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Can $this be Used Outside Class (PHP)." shoaiyb sysa | Sciencx - Sunday November 21, 2021, https://www.scien.cx/2021/11/21/can-this-be-used-outside-class-php/
HARVARD
shoaiyb sysa | Sciencx Sunday November 21, 2021 » Can $this be Used Outside Class (PHP)., viewed ,<https://www.scien.cx/2021/11/21/can-this-be-used-outside-class-php/>
VANCOUVER
shoaiyb sysa | Sciencx - » Can $this be Used Outside Class (PHP). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/21/can-this-be-used-outside-class-php/
CHICAGO
" » Can $this be Used Outside Class (PHP)." shoaiyb sysa | Sciencx - Accessed . https://www.scien.cx/2021/11/21/can-this-be-used-outside-class-php/
IEEE
" » Can $this be Used Outside Class (PHP)." shoaiyb sysa | Sciencx [Online]. Available: https://www.scien.cx/2021/11/21/can-this-be-used-outside-class-php/. [Accessed: ]
rf:citation
» Can $this be Used Outside Class (PHP) | shoaiyb sysa | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.