Jump to content

[SOLVED] Basic question: how to fetch variable from class?


Darla

Recommended Posts

Hello

 

I have a class structure like this:

class test{

function fetchsomevalues()

{

$testvalue="1";

}

 

}

 

In another class I wish to use the values in the variables from the class test, I include the class and create a new object of it:

 

$fetcobj= new test;

$fetcobj->fetchsomevalues();

 

How can i access the variable $testvalue from here?

 

Darla

Link to comment
Share on other sites

$testvalue disappears after your function call. The variable you have has been declared with scope local to that function, but it dies beyond that. You might want something like this:

 

<?php
class test{
public $testvalue;
function fetchsomevalues()
{
$testvalue = 1;
}
}

$fetcobj = new test; //or should it be test()? Is this one of my Java irks coming out again?
$fetcobj->fetchsomevalues();
echo $fetcobj->somevalue; //output: 1
?>

 

It has to be declared outside of the function to have access to it, and it must be public to access it that way.

 

Also, using a function name like fetchsomevalues() to set a variable is a bad naming idea. The name makes the user think that it will return the value back, in which case you'd want to change fetchsomevalues to this:

<?php
function fetchsomevalues(){
$somevalue = 1;
return $somevalue;
}
?>

Link to comment
Share on other sites

Hei

 

Thanks both of you. The first answer did not return anything?

 

The second one using $this worked :) Super!

 

Darla

 

Just a note, as you get into PHP5 and start using some of the more detailed OOP principles, you'll have to be careful about just accessing the variables directly. You'll have to use some accessor methods to get at them. It's actually a very good practice to get into either way. This is what ToonMariner is getting at in his example, I believe. Take note of the following:

<?php
// PHP5 declaration
class myObject {
  private $value = 1;
  
  public getMyValue() {
    return $this->value;
  }
}

$obj = new myObject();

// this will work:
echo $obj->getMyValue();

// this will not, since the variable is "private":
echo $obj->value;
?>

 

Notice that in PHP4, accessing the variables directly (as in the second example above) will work since you can't declare variables with "public" and "private" in PHP4.

 

Hope this makes sense.

Link to comment
Share on other sites

Hello obsidian

 

Thanks for info. I tried testing the script you gave but received an error:

 

"Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in testphp5.php on line 6"

 

Seems to be an issue with this line:  public getMyValue() {

 

Any idea why?

Link to comment
Share on other sites

Try

public function getMyValue()

It should work the way it is, but better safe then sorry.

 

No, that's right. If you have only "public" before the function name, the parser is expecting a variable name, not a function declaration. "public function" is the appropriate way. That's what I get for trying to write up some quick notes ;)

Link to comment
Share on other sites

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.