PHP is an object-oriented programming language, which means that you can create objects, which can contain variables and functions.
<!DOCTYPE html> <html> <head> <title> Introduction to Object-Oriented Programming </title> <link 'text/css' rel='stylesheet' href='style.css'/> </head> <body> <p> <?php // The code below creates the class class Person { // Creating some properties (variables tied to an object) public $isAlive = true; public $firstname; public $lastname; public $age; // Assigning the values public function __construct($firstname, $lastname, $age) { $this->firstname = $firstname; $this->lastname = $lastname; $this->age = $age; } // Creating a method (function tied to an object) public function greet() { return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)"; } } // Creating a new person called "excellent 12345", who is 12345 years old ;-) $me = new Person('excellent', '12345', 12345); // Printing out, what the greet method returns echo $me->greet(); ?> </p> </body> </html>
0 Comments
Leave a Reply. |
AuthorVitali Kremez Archives
January 2016
Categories |