sfc Posted June 21, 2010 Share Posted June 21, 2010 public function __construct($student_name) { echo "Hello {$student_name}! <br />"; parent::__construct(); echo "The number of students in your class is: " . parent::$class_attendance . "<br /><br />"; self::$student_count++; } I want to access the $student_name variable through the instance but I don't know how... I tried this: echo "Student" . $student1->student_name . " is the same as student " . $student2->student_name.": "; echo ($student1 == $student2) ? "True" : "False"; but it didn't work. I am very new to OOP in PHP. What am I doing wrong? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/ Share on other sites More sharing options...
gizmola Posted June 21, 2010 Share Posted June 21, 2010 Is student_name a class variable? Post your full class definition for us to look at. Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075221 Share on other sites More sharing options...
sfc Posted June 21, 2010 Author Share Posted June 21, 2010 No. And I know that the way I am calling it right now is how you would call a Class variable, so I know it isn't right but I don't know how to call the variable inside the method. As you requested: class Student extends Classroom { static public $student_count = 0; public function __construct($student_name) { echo "Hello {$student_name}! <br />"; parent::__construct(); echo "The number of students in your class is: " . parent::$class_attendance . "<br /><br />"; self::$student_count++; } } Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075222 Share on other sites More sharing options...
sfc Posted June 22, 2010 Author Share Posted June 22, 2010 Was my question too stupid? Sorry, I'm still very new at this. Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075662 Share on other sites More sharing options...
Alex Posted June 22, 2010 Share Posted June 22, 2010 $student_name isn't a property of the Student class. I'm not sure but I think you want to do something like this: class Student extends Classroom { static public $student_count = 0; private $_student_name; public function __construct($student_name) { $this->_student_name = $student_name; echo "Hello {$this->_student_name}! <br />"; parent::__construct(); echo "The number of students in your class is: " . parent::$class_attendance . "<br /><br />"; self::$student_count++; } public function getName() { return $this->_student_name; } } $student1 = new Student('sfc'); echo $student1->getName(); By the way, your hierarchy doesn't seem to make sense. Why should student extend Classroom? If you want help with your logic with that as well post the class definition for Classroom. Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075670 Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 We are here to help people learn So, your class definition needs to have variables to store key pieces of information for later use. If you need to access the $student_name you need a class variable to store it in, and you should do that in your __constuct(). Then later you can access it using a get or, if you declare it to be public via direct access. class Student extends Classroom { public $student_name = ''; static public $student_count = 0; public function __construct($student_name) { echo "Hello {$student_name}! "; $this->student_name = $student_name; parent::__construct(); echo "The number of students in your class is: " . parent::$class_attendance . " "; self::$student_count++; } } Add the variable and set it in the __construct() like so, and you should then be able to do: echo $student1->student_name; Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075675 Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 FWIW, Alex W.'s advice is considered best practice from an OOP perspective, as it implements the concept of "Information Hiding". If you're studying OOP, it's a good idea to look carefully at the scope keywords: public, private and protected, and the ways they effect variables and class functions in regards to inheritance and via general use. Making the variable private means that it can't be accessed directly in the way I did, which is often considered a good thing by class designers who want to be able to change internal class data storage details without breaking applications that use the class. With PHP's loose typing this isn't as big an issue as it is with other languages, but at least you get an idea of why there's 2 different examples presented. Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075680 Share on other sites More sharing options...
Alex Posted June 22, 2010 Share Posted June 22, 2010 Yeah, I should have explained the code in my post, sorry about that. I was a little busy at the moment. gizmola pretty much covered everything Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075684 Share on other sites More sharing options...
sfc Posted June 22, 2010 Author Share Posted June 22, 2010 Thanks guys! That makes a lot of sense. I'll try that out. Quote Link to comment https://forums.phpfreaks.com/topic/205463-calling-a-passed-variable-in-a-method/#findComment-1075697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.