Full Transcript

·YouTLDR

OOP DASAR pada PHP #5 - Object Type

9:00EnglishTranscribed Jul 18, 2026
0:00

Hello friends, how are you? Hopefully you are all fine Welcome back to the Unpas web programming channel with me, Sandika Gali And this time we will continue our learning about the basic object-oriented on PHP This time in the fifth video we will discuss the object type on PHP

0:20

So, as you know, we know there are types of data in PHP. There are primitive data types such as integer, string, boolean, and there are also types of data formed such as array. This time we will try to use objects as one of the types of data that we can use in the class. Let's just try using the case study in our previous video.

0:49

So this is the result of our previous video where we have learned about the constructor method by using the method called __construct Now we will discuss a new topic called object type So how about we save the file first so it doesn't disappear Save as we change the name to object-type like this, just to differentiate

1:17

Now the story is like this, I want to make a new case like this So friends remember the story we have this class we use to store product data The product we use is specific, there are two, there are books and games The story is that I want to make a functionality to print complete information from the product I have

1:42

Well, actually there are many ways, you can make a method here or because we make this functionality only to print info, we can also use a separate class, so we will try to make a new class below here, even though you actually want to make a class in a separate file, you can also do it, this is for simple, I make it in the same file, for example,

2:09

I have a new class called "CetakInfoProduct" So this class only has one task, to type all product information, whether it's a book or a game And this class only has one method, which is the type method, like that Yes, friends, just follow it, we'll see later, the story is like that Later, this function will return the value of a string, like that

2:40

I write like this, I have a str variable filled with what string I want to display when I type info about a product So I want to display like this First I display the title first, for example I want to type a comic, Naruto Then there is the divider, then here there is who the writer is, then who the author is, then what is the price So I want to display it like this

3:12

So this class is only to print complete information from the object that I have. Now I can do it by accepting the parameter input for this method. Later the parameter is the object, the product that has been made here. So when the product is instantiated, it will become an object. We can take the object as a parameter for this method.

3:40

So we change it later, this is not Naruto anymore But we take the product, then we take the title You can also store it in a drawer like this So that you don't have to worry about joining strings You can store it in a drawer Then for the writer, we can actually make this Writer product

4:12

But remember, we already have the get label to display the writer and the reader. Okay, let's just make it here, get label. This is not needed anymore. Just in here, the Rp. 30,000, we change it to what? The initial value is the price product. So later I want the product info to be completely printed.

4:44

Okay, now if I run it, let's run it first, this is the third product, we delete it, we just use two, like this, we save, we go back to our browser, refresh, this is the object type, we click, yes, there is no error, but it hasn't appeared yet, we will show it later. So we have made a class, which later this class will receive object inputs. How to run this printing function, let's see here.

5:13

Now, for example, I want to print the complete information of Product 1 from this Naruto So the way we do it is enough like this, for example, I make a new variable called infoProduct1, for example, yes, now we instantiate this class, the class that prints infoProduct, so I just need to print infoProduct like this

5:41

This is for new class instantiation, but we don't know which product to print. If we want to print, we just call this. info.product1.print To call this method, we just enter the parameter. What is the parameter? You just have to determine which one you want to print, Naruto or Uncharted. If you want to print Naruto, just write $product1. If we echo, save, then we look here, we refresh.

6:16

Oh, it hasn't shown yet. Why hasn't it shown yet? Oh, yes, because we haven't returned it yet. We return it first. Back to the printing method, we return. What is it? $ST. Okay. Save first, go back here, refresh. Now, it's done. To make it neater, we give it a BR first. Let's see again. Now the label is complete. There is the comic name, the author and the price.

6:49

Done, this is actually finished. But if you look at this printing method, we receive the dollar product parameter. The hope of this dollar product is this object, right? But because we don't know what we have to put in here, what can we do? Yes, whether it's a string, integer, even other objects other than the product object can be.

7:15

So that this font function only receives specific product classes So only the product class that can enter here We just add in front of here is the type That is the product So the font function only receives parameters that are the type of the product class Then what is the object Okay, if we run this, it's the same, but when we enter it randomly like this

7:48

We run the error, see this, the error is correct, but the error is the parameter or the argument must be an instance of product So it is immediately told that what is entered must be an instance of the product class, other than that it is not allowed So this is an example that we can make an object as our own type of data

8:15

So that's it for the video material this time, we learned about the object type, hopefully you understand and for the next video we will discuss the concept of inheritance or what is called inheritance on the PHP. So this is the video, thank you for watching, I'm Sadeka Ghalipamit, see you again in the next video.

More transcripts

Explore other videos transcribed with YouTLDR.

Get the TLDR of any YouTube video

Transcribe, summarize, and repurpose videos in 125+ languages — free, no signup required.

Try YouTLDR Free