Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-------------------------------------
- What's Object-Oriented Programming?
- _______________________________________*/
- PHP is an object-oriented programming language, which means that you can create objects, which can contain variables and functions.
- When talking about objects, you refer to variables belonging to these objects as properties (or attributes or fields), and functions are called methods.
- These objects are essential when dealing with PHP, as almost everything is an object: for example, functions or arrays are objects, too!
- And this shows why we use objects: we can bundle our functions and data in one place, we can create objects easily using classes (object constructors), so we can create lots of instances (objects, which have been constructed via a class), which contain mostly the same data, except some little nuances.
- On the right, there is a Person class and one instance stored in $me on line 32. Then the greet() method of the $me object is called and the result is echod on line 35.
- <?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 "boring 12345", who is 12345 years old ;-)
- $me = new Person('boring', '12345', 12345);
- // Printing out, what the greet method returns
- echo $me->greet();
- ?>
- /*-------------------------------------
- Objects in Real Life
- _______________________________________*/
- How object-oriented programming is used in real life can be shown with a forum as an example:
- Every forum user (object) has the same rights: he can log in and write (methods), can contain some settings (properties), but every user has a different name (another property).
- Every user is created easily, as you create a new instance of a User class when you sign up. And as we've seen, there are some properties and methods that every instance has in common (such as logging in and writing), and there are some which are unique (such as each user's name).
- And without object-oriented programming—OOP for short—this could not be done that easily. ;-)
- Another example: on the right, there is a Person class, so every new Person has some properties, like $isAlive or $firstname, and a method greet().
- Right now there is only one instance of the Person class: $me. But we'll reconstruct this class and you'll even create another instance of the class, so your name will be echod, too.
- <?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 "boring 12345", who is 12345 years old ;-)
- $me = new Person('boring', '12345', 12345);
- // Printing out, what the greet method returns
- echo $me->greet();
- ?>
- /*-------------------------------------
- Building Your First Class
- _______________________________________*/
- <?php
- /*
- * The basic class syntax looks like the following
- * The class keyword means that you create a new class; the syntax is quite similar to the function syntax.
- */
- class Person{
- }
- // Create new instances(objects) of this class using the following syntax
- $teacher = new Person;
- $student = new person;
- /*
- * The new keyword means that you create a new object and ensures that your arguments are added as properties,
- * so it initializes the constructor (which we are going to deal with later)
- * We don't need to pass in any arguments, as we haven't added any properties (which can store different values depending on the instance) quite yet.
- */
- ?>
- /*-------------------------------------
- Property Panic (1)
- _______________________________________*/
- Properties are pieces of data bound to an object, and you can imagine an object as a bundle of information and actions.
- <?php
- //we first create a new class called 'Person'.
- class Person{
- //we add a property, $isAlive, and set its value to 'true'.
- public $isAlive = true;
- //we add three property, $firstname, $lastname & $age but don't store anything in it yet.
- public $firstname;
- public $lastname;
- public $age;
- }
- //we create new instance of Person and store it in $teacher & $student.
- $teacher = new Person();
- $student = new person();
- //Then we set the $firstname property of $student to the string "Sayful".
- $teacher->firstname = "Sayful";
- //Finally, we print out the two properties of $teacher.
- echo $teacher->isAlive;
- echo $teacher->firstname;
- ?>
- /*-------------------------------------
- Property Panic (2)
- _______________________________________*/
- Now we have some properties.
- But right now $teacher and $student are the same, which should be changed immediately, correct? :-)
- The solution:
- we have to create a constructor to create different objects. This constructor is also a method, but you don't need to worry about this fact just yet.
- The syntax:
- <?php
- public function __construct($prop1, $prop2) {
- $this->prop1 = $prop1;
- $this->prop2 = $prop2;
- }
- ?>
- So you should remember the public keyword and the arrow notation.
- Some new things:
- 01. You're creating a function bound to a class (a method).
- 02. The constructor method has to be called __construct().
- 03. Finally, the weird way to assign the values: $this->prop1 = $prop1 means that the value you pass in the __construct() function via the new keyword is assigned to $this, which represents the object you are dealing with, and ->prop1 is the actual property of the object.
- 04. By creating a new instance using the new keyword, you actually call this __construct() method, which constructs the object. And that's why we have to pass in some arguments when we create an instance of a class, since this is how the properties get set!
- <?php
- class Person{
- public $isAlive = true;
- public $firstname;
- public $lastname;
- public $age;
- // Use the three parameters to set the public properties $firstname, $lastname and $age
- public function __construct($firstname,$lastname,$age){
- $this->firstname = $firstname;
- $this->lastname = $lastname;
- $this->age = $age;
- }
- }
- //Change your $teacher instantiation to store new Person("Sajeeb", "Hossain", 22)
- $teacher = new Person('Sajeeb','Hossain',22);
- $student = new person('Sayful','Islam',26);
- //echo the $firstname of $teacher
- echo $teacher->firstname;
- //echo the $age of $student
- echo $student->age;
- ?>
- /*-------------------------------------
- A Method to the Madness
- _______________________________________*/
- Great work, now the hardest and longest part is behind us. :-)
- As you've seen, methods—functions bundled into objects—have the following syntax:
- <?php
- public function funcname($optionalParameter) {
- // Do something
- }
- ?>
- And now we know the __construct function is a special one, which is called when a new object is created using a new keyword.
- Furthermore, we've learnt we have to use the $this keyword, if we want to access some properties in a class.
- So if we want a method to return a sentence containing the firstname, we would have to use $this->firstname. (As you see, there is no $ when you access a property in a class.)
- Calling a method is similar to accessing a property, you just have to add the parentheses:
- $obj1 -> meth1();
- <?php
- class Person{
- public $isAlive = true;
- public $firstname;
- public $lastname;
- public $age;
- public function __construct($firstname,$lastname,$age){
- $this->firstname = $firstname;
- $this->lastname = $lastname;
- $this->age = $age;
- }
- public function greet(){
- return "Hello, my name is ".$this->firstname." ".$this->lastname.". Nice to meet you!";
- }
- }
- $teacher = new Person('Sajeeb','Hossain',22);
- $student = new person('Sayful','Islam',26);
- echo $teacher->greet().'<br>';
- echo $student->greet();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement