Gå til innhold

OO-PHP - kalle en metode i kontstruktør


Anbefalte innlegg

Hei folkens..

 

Jeg er relativt ny innen PHP, men mener dette skal være mulig i Java, så forstår ikke helt at dette skal være en begrensning i PHP...

 

<?php

/*
* Class User - Represents a user. Basic model class
*/
class User {
private $name;
private $pic;
private $email;
private $isConfirmed;
private $role;

/*
 * Constructor - if there is no picture associated to the user, please send null to the constructor
 */
function __construct($name, $pic, $email, $isConfirmed, $role) {
	$this->name = $name;
	//TODO check if file exists
	$this->pic = $pic;	
	if(validateMail($email)) {
		$this->email = $email;
		$this->isConfirmed = $isConfirmed;
	}
	else {
		throw new Exception('mailNotValid');
		$this->email = 'INVALIDMAIL';
		$this->isConfirmed = false;
	}
	$this->role = $role;
}


/*
 * getName() - returns the name
 */
function getName() {
	return $this->name;
}

/*
 * setName() - sets the name.
 */
function setName($name) {
	$this->name = $name;
}

/*
 * getPic() Returns the filename of the picture, if not set it returns null
 */
function getPic() {
	return $this->pic;
}

/*
 * setPic() - remeber, send it the filename as a string
 */
function setPic($picFileName) {

	//TODO check if file exists
	$this->name = $picFileName;
}

/*
 * getEmail - returns the mail address
 */
function getEmail() {
	return $this->email;
}

function setEmail($mailAddress) {
	if(validateMail($mailAddress)) {
		$this->email = $mailAddress;
	}
	else throw new Exception('mailNotValid');
}

function isConfirmed() {
	if($this->isConfirmed) {
		return true;
	}
	else return false;
}

function confirm() {
	$this->isConfirmed = true;
}

function getRole() {
	return $this->role;
}

/*
 * validateMail - a function for validating a mail address using regular expressions
 * found on http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm
 */
function validateMail($mail) {

	if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
		return true;
	}
	else return false;
}


}
?>

 

Et kall til konstruktøren fra en testfil gir meg: Fatal error: Call to undefined function validatemail() in /Applications/MAMP/htdocs/COAM/COAM-CMS/mvc/model/User.php on line 33

 

Hvor line 33 selvsagt er der hvor jeg bruker validateMail() i konstruktøren.

 

Hvordan skal jeg løse dette?

Lenke til kommentar
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...