PHP Object Oriented Programming (OOP): Classes (2/13)

preview_player
Показать описание

Official site

Twitter
Рекомендации по теме
Комментарии
Автор

For those who found the explanation about why the $person variable in the Person class was an error and that it needed to be $this instead, (I found it unclear the first time, the toning of when he said "Okay. Here we need to change this 'person' variable to 'this'. And basically what 'this' means is this current instance." was a little off so I had to re-watch it a few times to understand) here's a little more information about it.

The reserved keyword $this is a reference to the current instance or object. Let's say you have this class:

class Car
{
    public $brand;
    public $model;

    public function identify()
    {
        return 'This is a ' . $this->brand . ' ' . $this->model;
    }
}

Which are instantiated like so:

$car1 = new Car;
$car1->brand = 'Opel';
$car1->model = 'Astra';

$car2 = new Car;
$car2->brand = 'Nissan';
$car2->model = 'Micra';

Using the following line:

echo $car1->identify();

will result in:

This is a Opel Astra

$this is actually a reference to $car1 in this case.

I hope this clears things up for the ones that didn't quite follow, like I did.

# Edit, "Class" should in lowercase... oops.

DaBananaboat
Автор

Oh, I get it now. The stdClass is for defining objects that don't have a class. You probably mentioned it the last video, but I couldn't get my head around it until I saw the example of the person object being defined from the Person class.

NoahNobody
Автор

at last someone made a video for me. After wasting lot of time trying to get what this is all about. Thanks, hopefully I can learn oop now

odunladeoluwaseun
Автор

Nice for you to add subtitles. Makes me more understandable

cjvaans
Автор

Good job! Well done! Like for all the videos.

snaixs
Автор

what editor so u use, new to mac. for the php of course

Iamherp
Автор

class person 
public $name;
public $age;

$person = new person
$person->zzname = 'John123';
echo $person->zzname;

Returns: John123

What is the point of declaring $name in the class, if we can assign any property to the person object via zzname?

charlietann
Автор

how do you make sublime ask you to name a new project when you open it?

Isaacstation
Автор

You are amazing man you explained everythings well thx alot...

ihabs
Автор

is the sign "->" in php same as "." in java?

minhhoang
Автор

I' am stil getting a syntax error, unexpected '$person' (T_VARIABLE)

shahinmolawi
Автор

Is that Sublime? What's the theme? :D Thx

DarKcS