I'm having trouble accessing a class' variable.
I have the functions below in the class.
class Profile {
var $Heading;
// ...
function setPageTitle($title)
{
$this->Heading = $title;
echo 'S: ' . $this->Heading;
}
function getPageTitle2()
{
echo 'G: ' . $this->Heading;
return $this->Heading;
}
// ...
}
Now when I run the method $this->setPageTitle("test")
I only get
G: S: test
What's wrong with the getPageTitle2
function? Heading is public btw. Please help!
Thanks guys!
-
you have to declare the Heading and title out of the function ... i dont know if you already did that
see the order of calling the functions
Fergs : yes, my class starts like: class Profile { var $Heading; but does $title need to as well even though its feed straight into the function?antpaw : no its a anonymous variableFrom peacmaker -
If you have "G: S: test" it means you called getPageTitle2 before setPageTitle ! It looks normal then : I suggest first set then get.
From Eric -
Now when I run the method
$this->setPageTitle("test")
I only getG: S: test
That sounds implausible. Are you sure you're not running:
$this->getPageTitle2(); $this->setPageTitle("test");
PHP - like most programming languages - is an imperative language. This means that the order in which you do things matters. The variable
$this->Header
is not set at the time where you callgetPageTitle2
.cballou : sounds likely to me..Fergs : well i have other functions //(the only one that calls the set function) function getPageContents() { $this->setPageTitle("test"); } and the other function setContent // runs the getPageTitle2 { echo ''.$this->getPageTitle2().''; } so i didn't think it mattered the placement of functions within a class. Perhaps the getPageContents() must be placed above the Set()?meagar : @Fergs Placement of the method in the class doesn't matter, this I can guarantee. Somewhere in your code, you are explicitly calling getPageTitle2(), before you call setPageTitle() - there is no implicit or otherwise automagical way to call a method named getPageTitle2.Fergs : thats what i figured: i;ve uploaded the code to www.fearghal.com/Untitled-1.phps Its semi large but the SetContent and getPageContents are in there and call the functions in question...could you have a look?troelskn : @fergs That code is just a class. It won't do anything at all. Your problem likely lies in the code that *uses* the class.From troelskn -
class Profile {
var $Heading; // ... function setPageTitle($title) { $this->Heading = $title; echo 'S: ' . $this->Heading; } function getPageTitle2() { echo 'G: ' . $this->Heading; return $this->Heading; }
// ... }
I am guessing you are doing something like this:
$profile = new Profile();
$profile->setPageTitle("test"); $profile->getPageTitle2();
and that this would result in the following output:
S: testG: test
and that if you echo $profile you will just get
test
so what do you think is the problem or what are you not accomplishing that you want to?
also I would probably declare $Heading as
private $heading;
From kelly -
If I'm not mistaken you're asking the same question as one I asked a few months ago. http://stackoverflow.com/questions/1618766/php-class-arguments-in-functions Should get you what you want!
Hope I helped
Giles
From Giles Van Gruisen
0 comments:
Post a Comment