Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Brenda Krafft
18,036 PointsAble to call "set_value" function in a class, but "get_value" function fails.
I have a User class defined in one php file:
class User {
var $emp_id;
function set_emp_id($id) { $emp_id = $id; }
function get_emp_id() { return $emp_id; }
}
In my index.php file I include the above file and create an instance of the user, like so:
$user = new User();
$user->set_emp_id($employee["emp_id"]);
That works fine. BUT when I try to get the value in index.php later:
echo "emp ID: " . $user->get_emp_id() ;
I get an error tellng me that $emp_id is an undefined variable in my get_emp_id() function.
I know that $emp_id is getting set, because I can echo that out from the "set_emp_id()" function. I also checked that my $user class existed with an "isset($user)" check, and it returned true.
So the $user class exists, I set a value with the set function, but the get function doesn't recognize the only variable in the User class. UGH!
1 Answer
simhub
26,544 Pointshi Brenda,
you confuse javascript with php, i guess.
there is no "var" in php, as far as i know.
you could try this:
User_class.php
class User {
//set private or public variable
private $emp_id;
//set your $emp_id variable equal to what ever you put inside the param.
function set_emp_id($id) { $this->emp_id = $id; }
//return private or public variable
function get_emp_id() { return $this->emp_id; }
}
index.php
include "User_class.php";
$user = new User();
$user->set_emp_id("emp_id");
echo "emp ID: " . $user->get_emp_id() ;
Brenda Krafft
18,036 PointsBrenda Krafft
18,036 PointsThanks!