PHP Classes – How to Create a Class with a Method Function


class PHPClassExampleMethod {

/*Define your variables*/
var $fname;
var $lname;

/*Create our custom function and indicate
it will be accepting two parameters.*/
function ShowInfo($fname,$lname) {

/*Passed parameters need to be assigned
values so we can use them.*/
$this->fname = $fname;
$this->lname = $lname;

/*This is how we will output the value.
We concatenate strings and values.*/
echo "The name of the person is " . $this->fname;
echo "Their last name is " . $this->lname;

}

}

//create a class
$myPHPClassWithMethod = new PHPClassExampleMethod;

//Call the method and pass two parameters
$myPHPClassWithMethod->ShowInfo("James", "Tadeo");

?>

Testimonials

"Okay, obviously you're showing off here. NOBODY does turnaround times measured in minutes. = : ) Thanks again."
Jim Walker, James Walker Home Inspections
"THANK YOU JAMES YOU ARE INCREDIBLE! My sincerest thank you I really can't thank you enough..."
Bruno Marsala, HDMedia
"James has been a pleasure to work with. He gets stuff done efficiently, and listens to my every little gripe and comment. He's very good and I highly recommend him. "
Robert Nay, Nay Games LLC