...

View Full Version : Function and class



Crisp
02-02-2010, 05:08 AM
Hey,
I'm trying to echo a variable from a different function within the class.
How can I do this?
This is what I've got, just for a quick example:

class UserPermissions
{
//Post Thread permission
function Permissions()
{
$test = "Hey"; //The variable
}
function PostThread()
{
echo $test; //Now I want to echo the variable above
}
}Thanks!

kbluhm
02-02-2010, 05:09 AM
class UserPermissions
{

private $test; // can also be `public` or `protected`

//Post Thread permission
function Permissions()
{
$this->test = "Hey";
}

function PostThread()
{
echo $this->test;
}

}

Crisp
02-02-2010, 05:12 AM
Hmm, doesn't seem to echo it.
I'll mess around with it though if you don't know the problem.

Thanks.

Dormilich
02-02-2010, 05:15 AM
the class looks all right.

kbluhm
02-02-2010, 05:15 AM
I don't know the problem because we haven't seen any code aside from the actual class. Let's see something.

Crisp
02-02-2010, 05:17 AM
Ok:
$user_permission = new UserPermissions();
$user_permission->PostThread();

abduraooft
02-02-2010, 05:19 AM
Hmm, doesn't seem to echo it.
I'll mess around with it though if you don't know the problem.

Thanks.
You need to call those functions from an object of that class.

kbluhm
02-02-2010, 05:20 AM
Ok:
$user_permission = new UserPermissions();
$user_permission->PostThread();

Where are you calling the Permissions() method? Since that is the method assigning the value, if you don't call that the value will not be asigned.

Crisp
02-02-2010, 05:21 AM
@abduraooft (http://www.codingforums.com/member.php?u=47830) I'm not really sure what you mean.
I'm not to good at classes yet because I'm still learning them and am kind of new to them.

Anyways, thanks for the reply,

@kb, oh dang, I forgot to do that!
Let me try it now.

Crisp
02-02-2010, 05:24 AM
Alright, it works now!
Thanks man!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum