PHP : call variable from another function in class - php

This is my class code:
class myClass
{
public function myFunc()
{
$myvar = 'Test str';
}
public function result()
{
echo myClass::myFunc()->$myvar;
}
}
and I use this:
$nCls = new myClass;
$nCls->result();
To show Test str form myFunc() but nothing shown. I think the problem is :
echo myClass::myFunc()->$myvar;
Thanks for any help.

You are mixing up quite a few concepts.
First, you have to create a new object of class myClass:
$nCls = new myClass();
Then, you can call the member function (method) on that class:
$nCls->result();
In result(), you just call the other method using $this:
public function result()
{
echo $this->myFunc();
}
Note though that this does nothing. The variable $myvar is local and not a class attribute. I advise you read up on object oriented programming, and object oriented PHP in particular.

class myClass {
public $myvar;
public function myFunc() {
$this->myvar = 'Test str';
return $this;
}
public function result() {
echo $this->myFunc()->myvar;
}
}
$nCls = new myClass;
$nCls->result();
You can do this but this is not a good practice.

The problem is that you declare $myvar only in the scope of method myFunc(). That means it is not visible outside that method. Declare it as a class member instead:
class myClass
{
private $myvar;
public function myFunc()
{
$this->myvar = 'Test str';
}
public function result()
{
echo myClass::myFunc()->$myvar;
}
}

the problem is the scope, you can't call a variable within another function, define a property for the class and set it from a function then retrieve the property with result():
class myClass
{
public $myvar;
public function myFunc()
{
$this->myvar = 'Test str';
}
public function result()
{
echo $this->myvar;
}
}

include "views.php";
class Controller extends views{
function index(){
$this->head();
$this->home();
}
function privacy(){
$this->head();
$this->privc();
}
function about(){
$this->head();
$this->abt();
}
function address(){
$this->head();
$this->add();
}
}
$obj=new Controller();
if(isset($_GET['fun'])){
$obj->$_GET['fun']();
}
else{
$obj->index();
}
This is views.php code
class views{
function head(){
include "views/header.php";
echo "<br>this is header";
}
function abt(){
include "views/about.php";
echo "<br>This is about us page";
}
function home(){
include "views/home.php";
echo "<br>This is Home page";
}
function privc(){
include "views/privacy.php";
echo "<br>This is privacy page";
}
function add(){
include "views/address.php";
echo "<br>This is address page";
}
}

Related

How to call a function of child class inside a function of parent class

I want to call a function defined in child class from a function defined in the parent class for some logic. I am getting error of undefined function. How can I call this function. Here is my sample code.
<?php
class First
{
public function __construct()
{
echo "First class is initiated.";
}
public function call_child()
{
$this->get_ouput();
}
}
class Second extends First
{
public function __construct()
{
parent::__construct();
}
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
?>
The issue is spelling mistake in your function call name
<?php
class First
{
public function __construct()
{
echo "First class is initiated.";
}
public function call_child()
{
$this->get_output(); //update the name here
}
}
class Second extends First
{
public function __construct()
{
parent::__construct();
}
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
?>
There is a typo when calling the get_output method. However you should define the First class and get_output method as abstract in order to be extended/implemented by the other classes:
abstract class First
{
public function __construct()
{
echo "First class is initiated.";
}
abstract public function get_output();
public function call_child()
{
$this->get_output();
}
}
class Second extends First
{
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
Note: the Second class may not define the constructor if it only calls the parent constructor.

how can I use of a class property in the nested function

Why a class property is not accessible in the nested function ? So how do I use it?
class MyClass
{
public $var = "I'm a class property!";
public function Test()
{
function SubTest()
{
// I need to $this->var; here
}
}
}
I want to echo $var in the SubTest(). Is it possible ?
How about using a closure? $this is visible inside the closure since PHP 5.4.
And besides that you should not be defining a function inside a method like this. PHP functions are global, means they behave in exactly the same way as if they had been declared outside.
class MyClass
{
public $var = "I'm a class property!";
public function Test($str)
{
$callback = function() use ($str)
{
print_r($this->var . " " . $str);
};
return call_user_func($callback);
}
}
$a = new MyClass();
$a->Test("My name is not Foo.");
What about doing this?
<?php
class MyClass{
public $var = "I'm a class property!";
public function Test(){
function SubTest(&$father){
$father->var = 'something else';
echo 'here ! '.$father->var.'<br />';
}
echo $this->var.'<br />';
SubTest($this);
echo $this->var.'<br />';
}
}
$test = new MyClass();
echo $test->var.'<br />';
$test->Test();
?>
Gives this result:
I'm a class property!
I'm a class property!
here ! something else
something else

How to use $this in outside of class?

Can we use $this outside of class. Please look at the example below,
<?php
class Animal {
public function whichClass() {
echo "I am an Animal!";
}
public function sayClassName() {
$this->whichClass();
}
}
class Tiger extends Animal {
public function whichClass() {
echo "I am a Tiger!";
}
public function anotherClass() {
echo "I am a another Tiger!";
}
}
$tigerObj = new Tiger();
//Tiger::whichClass();
$this->anotherClass();
Here I have created new object $tigerObj = new Tiger(); after that I tried to use $this but it throwing error. So is that possible to use $this from outside of the class ? If no,
$this refers to the current object. So why don't we use this ?
Its not possible to use $this in this way, you can create object of that class and then extend the methods which you would like to call. See below ...
class Animal {
public function whichClass() {
echo "I am an Animal!";
}
public function sayClassName() {
$this->whichClass();
}
}
class Tiger extends Animal {
public function whichClass() {
echo "I am a Tiger!";
}
public function anotherClass() {
echo "I am a another Tiger!";
}
}
$tigerObj = new Tiger();
echo $tigerObj->anotherClass();
You will get result "I am a another Tiger!"
$this is impossible to use outside class so you can make static method, and use like this Tiger::anotherClass. Link to doc
class Animal {
public function whichClass() {
echo "I am an Animal!";
}
public function sayClassName() {
$this->whichClass();
}
}
class Tiger extends Animal {
public function whichClass() {
echo "I am a Tiger!";
}
public static function anotherClass() {
echo "I am a another Tiger!";
}
}
$tigerObj = new Tiger();
//Tiger::whichClass();
Tiger::anotherClass();
NO you can't use $this outside the scope of a class
example :
1 $this=new \DateTime();
2 echo $this->format('r');
generates the following error :
Fatal error: Cannot re-assign $this on line 2
Yes it's possible to use $this outside class with php 8.
Example: class.php
<?php
class MyClass {
private $say = '';
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('Using this Outside Class Is Possible');
?>

object intitialization in method

Can I initiate an object instance and call a method inside another method in php?
Please give me examples at this question.. and show me separately all the combinations that can be done with methods and objects.
class Autorizare{
public function user(){
echo "This is a user";
}
}
class Derivata{
function derivata(){
echo "sunt o derivata";
}
$object = new Autorizare();
$object->user();
$object2 = new Derivata();
$object2->derivata();
}
echo $object->user();
echo $object2->derivata();
Something like this..
You can access methods of some class in these ways:
class A {
public function one() {
echo "This is a user";
}
public static function two() {
echo "This is not a user";
}
public function test() {
$this->one(); //for non static methods
self::two(); //for static methods
}
}
class B {
public function test1() {
$a = new A();
$a->one(); //for non static methods
}
public function test2() {
A::two(); //for static methods
}
}
$obj = new B();
$obj->test1();
$obj->test2();
For more details and also how to use scope look at php manual
*edit code upon comment request

PHP: Variable scope in OOP?

Here's my code:
class Manual extends controller {
function Manual(){
parent::Controller();
$myVar = 'blablabla';
}
function doStuff(){
echo $myVar; // Doesn't work.
}
}
I've tried various methods to make it work, but I've can't get my head around it. What can I do?
Thanks
In your code, $myVar is local to each method.
Perhaps you meant $this->myVar?
You need to use the $this 'pointer'.
e.g.:
class Test
{
protected $var;
public function __construct()
{
$this->var = 'foobar';
}
public function getVar()
{
return $this->var;
}
};
class Manual extends controller {
private $myVar;
function Manual(){
parent::Controller();
$this->$myVar = 'blablabla';
}
function doStuff(){
echo $this->$myVar;
}
}
Even more OOP-like with Setters/Getters
class Manual extends controller {
private $myVar;
function Manual(){
parent::Controller();
setMyVar('blablabla');
}
function doStuff(){
echo getMyVar();
}
function getMyVar() {
return $this->myVar;
}
function setMyVar($var) {
$this->myVar = $var;
}
function doStuff(){
echo $this->myVar;
}
The variable $myVar should be property of a class, and you can not do:
echo $myVar;
You should do:
$this->myVar;
As written, $myVar is local to both methods.
You need to declare $myVar as a property in the class body
protected $myVar;
and then use the pseudo variable $this to access the property in methods, including the constructor
$this->myVar;
$myVar field must be declarated as public/protected in the parent class or declarated in the descedent class, and in yours doStuff() method you must write $this->myVar not the $myVar

Categories