Jump to content

Calling a passed variable in a method


sfc

Recommended Posts

 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!

Link to comment
Share on other sites

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++; 

 }

}

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

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.