0

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!

2
  • Please provide code which call the methods. Commented Jan 6, 2010 at 14:00
  • the whole code is located at www.fearghal.com/Untitled-1.phps the function names are SetContent() and getPageContents() ... they called the functions is questions. Commented Jan 6, 2010 at 14:44

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 '<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 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.
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?
@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.
1

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

Comments

0

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

1 Comment

yes, my class starts like: class Profile { var $Heading; but does $title need to as well even though its feed straight into the function?
0
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.