Jump to content

Recommended Posts

I am trying to access a variable through a class, and can't work out how to this.

class test {

	function method() {
		if (isset({$testing})) {
			echo {$testing};
		}
	}

}

$testing = 'it works';
$thetest = new test;
$thetest->method();

 

Is this even possible, or have I got very confused?

It's both possible, and you're very confused.

 

In PHP, methods and functions are essentially the same thing (notice how they start with the function keyword).  How do you pass a variable to a function?  Through its argument list.  The same thing applies here:

 

 

class test
{
    public function method($x)
    {
        echo $x;
    }
}
 
$testing = 'it works';
$test = new test;
$test->method($testing);

 

EDIT: Note that while methods and functions are essentially the same mechanically, OOP is NOT merely placing a bunch of functions in a class and calling it a day.

Edited by KevinM1

Ok so what if the variable may not be set?

class test
{
    public function method($x=null)
    {
       if(isset($x)) echo $x;
    }
}
 
$testing = 'it works';
$test = new test;
$test->method($testing);

Would that work?

Edited by 1internet

Yea, I do, but sometimes I just like to make sure there isn't some standard or much simpler way to do things. It didn't work though.

So I think this is the best way

class test {

	function method($x) {
			echo $x;
	}

}

$thetest = new test;
if (isset($testing)) $thetest->method($testing);

Edited by 1internet

<?php

class Test {
    public $greetings; // Public Variable can be used anywhere

    public function my_world($x) // Global Method
    {
        echo "Good Afternoon, " . $x . "!<br />";
        return $this->greetings = "Have a Great Day " . $x . "!<br />";  // returns a varialble to the calling method
    }

}

$thetest = new Test; // Creat a new instance
$goodbye = $thetest->my_world("Kevin"); // Call the Method
echo $goodbye; //display it.
?>

 

<?php

class Test {
    public $greetings; // Public Variable can be used anywhere

    public function my_world($x) // Global Method
    {
        echo "Good Afternoon, " . $x . "!<br />";
        return $this->greetings = "Have a Great Day " . $x . "!<br />";  // returns a varialble to the calling method
    }

}

$thetest = new Test; // Creat a new instance
$goodbye = $thetest->my_world("Kevin"); // Call the Method
echo $goodbye; //display it.
?>

 

1. Public does not mean global.

2. Methods/functions really shouldn't echo and return.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.