tags:

views:

76

answers:

5

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!

A: 

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

peacmaker
yes, my class starts like:class Profile { var $Heading;but does $title need to as well even though its feed straight into the function?
Fergs
no its a anonymous variable
antpaw
+1  A: 

If you have "G: S: test" it means you called getPageTitle2 before setPageTitle ! It looks normal then : I suggest first set then get.

Eric
+4  A: 

Now when I run the method $this->setPageTitle("test") I only get

G: 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 call getPageTitle2.

troelskn
sounds likely to me..
cballou
well i have other functions //(the only one that calls the set function)function getPageContents() {$this->setPageTitle("test");}and the otherfunction setContent // runs the getPageTitle2{echo '<div id="page_title">'.$this->getPageTitle2().'</div>';}so i didn't think it mattered the placement of functions within a class. Perhaps the getPageContents() must be placed above the Set()?
Fergs
@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.
meagar
thats what i figured: i;ve uploaded the code to www.fearghal.com/Untitled-1.phpsIts semi large but the SetContent and getPageContents are in there and call the functions in question...could you have a look?
Fergs
@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.
troelskn
A: 

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;

kelly
A: 

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

Giles Van Gruisen