What’s Object-Oriented Programming? PHP (Codeacademy) explanations

What’s Object-Oriented Programming? PHP (Codeacademy) explanations

Let’s see, here are some explanation for the main object from the PHP course from Codeacademy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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(); 
        ?>

So we have a generic class, we have the function to “__construct”, and we have the method. A method will “do something”, is basically a function.

To do something with the class, we generate data for our class.

We call $me = new Person(“name”, “124”, “123”);
and to call the method greet from the function, we use this syntax.

echo $me->greet(); since $me is the variable that we used to add the data into our class/object.

Add a comment: