Jump to content

problem with private method


george
 Share

Recommended Posts

In the class below, I want to validate a student id when the class attempts to set it.It does not work, which is why I have those lines commented out. The class works with the attempted validation commented out. Also, further down, where I have "public function validate", My sence tells me that should be a private method because it is only used within the class. But when I change that from public to private, the class fails.

class student extends person implements iStudent {public $StudentID;public $Gender; public function setStudentID($StudentID) {//  if (validate( $StudentID )) {   $this->StudentID = $StudentID;//  } else {//   echo "invalid student id $StudentID";//  }}public function setGender($Gender) {  $this->Gender = $Gender;}public function printinfo() {  if (isset($this)) {   echo '<Student><br />';   // echo get_class($this).'<br />'   echo '   ID '.$this->StudentID.'<br />';   echo '   Name '.$this->name.'<br />';   echo '   Gender '.strtoupper($this->Gender).'<br />';   echo '   Birthday '.$this->BirthDate.'<br />';  } else {			echo "\$this is not defined.\n";		}} public function validate( $StudentID ) {  if (isset($StudentID)) {   echo $StudentID;   if (preg_match("/^(99|88)([0-9]{5})([A-Z]{2}$)/", $StudentID )) {	return true;   } else {	return false;   }  } else {			return false; "\$StudentID is not defined.\n";		} }}

Edited by george
Link to comment
Share on other sites

Does person have a public validate()? If so, then any derivatives must also be public.Also, the line you've commented will probably produce a fatal error if uncommented.

if (validate( $StudentID )) {

should be

if ($this->validate( $StudentID )) {

Link to comment
Share on other sites

Thanks! Adding $this-> when calling the validate method solved all the problems. Still, shouldn't the validate method be private, since it is used only within the class? When I change it's visibility to private,(and that of it's parent interface), the class will not run.

Link to comment
Share on other sites

If both person and student have a validate method, yet this method is not used outside of the class, it makes more sense to have this method as protected.

When I change it's visibility to private,(and that of it's parent interface), the class will not run.
Wait... parent interface or parent class? It's different... interfaces can only define public methods classes must have.Turn on error message display, and when things fail, tell us the message you see. Diagnostics are much more easier when they're illuminated like that.
Link to comment
Share on other sites

interfaces can only define public methods classes must have
That explains my error - thanks again. The reason I have the validate method in the student interface is that it applies only to the student class, not the person class. I just tried removing reference to the validate method altogether from the student interface, and then made the sudent class method protected, and that worked like a champ. Thanks again. Can I send you folks at W3 forums some cookies or something?
Link to comment
Share on other sites

BTW: How do I turn on error message display?I can not set directives in the php.iniIn my code I have error_reporting(E_ALL)but something more is needed.I need something that I can put in the code that will override the php.ini just for the class I am, working on.

Link to comment
Share on other sites

Add

ini_set('display_errors', 1);

and the errors should become visible.

Can I send you folks at W3 forums some cookies or something?
Should one of us visit the other's country and city, you owe me a beer ;) .

If I was to collect all such debts, I'd have beers for a week of party nights :lol: ...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...