Object Oriented PHP #10 - Clone & Destruct

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·6y ago
Skills: API Design60%

Key Takeaways

Covers clone and destruct methods in object-oriented PHP

Full Transcript

okay that my friends so in this video I'd like to talk about two what's known as magic methods in PHP now we've already actually seen a magic method so far in this series and that is this construct function right here now you can tell something's a magic method because it has two underscores inference of the name so the two other magic methods I want to talk about in this video are going to be the destruct method which is kind of the opposite of the construct method right here and also the clone method so what I'm going to do is first of all define a function for the destruct magic method so let's say public function and underscore underscore destruct now this destruct magic method this is used to perform any kind of cleanup or run any final code whenever the last reference to an object instance is removed so say I was to create a new instance of this user then assign that to a variable that variable is just a reference or a pointer if you like to that actual instance we've created and when that reference variable is removed if there's no other reference variable pointing to that instance then PHP will run this destruct method of that instance and then get rid of it so we've created this destruct method right here and inside all we're going to do is just echo something out so we'll just say echo and then we'll say the user and then whatever this instance username was was removed okay so now we have that method settled but the minute it's not going to do anything we have to figure out how we can remove the reference to a variable so how do we do that well there's two ways really first of all we can use the unset function so say for example we've got a reference to this instance in user 1 I could unset that by saying unset and then pass in the reference which is user 1 and that is going to basically remove that reference to the instance of the user and because that was the only reference to that instance we don't have this user stored in another variable we're only different users then because that was the last one it's then going to run this destruct method right here echo this to the screen and then totally remove the instance itself so let me now save this and preview because we should see this echoed on the screen but what you see actually might surprise you so let me refresh and we can see the user Mario was removed but also the user Yoshi was removed and also the user Luigi was removed now first of all let me just add on a br tag here so they all go on a separate line and refresh but this is expected because we actually unset this reference right here so that was the first one and we expect the destruct method to run but it's also running for these two things right here so why is that well when a PHP file finishes executing when it gets to the end of the code automatically these references to these instances are removed because we don't need them anymore and therefore since they're removed and that was the last reference to them this runs as well so we don't actually need to manually unset that right here because we get to the end of the file and automatically these are going to be removed anyway these instance references so if we say this is going to have no effect so we can see right here this all still works so that's the first magic method I wanted to show you the next one is called the clone method the clone method is going to run when an object instance is cloned and we can use it to perform a bit of object setup if needed on that new cloned instance so we could clone an instance of an object that we already have by saying something like clone and then whatever instance we want to clone so for example user 1 okay and that is going to grab this object and it's going to clone it now where's it going to store that well in a new variable so user 4 is equal to clone user 1 so we get an identical copy of this object now stored inside user 4 as well now when this event happens when something is cloned like this we can tap into the clone magic method on the instance which is newly created using that clone so let me now go up here and I'm going to just in fact know I won't comment that out I'll keep it and I'll create a new one public function underscore underscore because this is a magic method Chlo so this clone function runs for the instance which has basically just been created using clone so it would run for this instance right here so we could use it to perform some kind of setup when we first create this object because by default we're going to get all the same properties as use of one because that's what we're cloning so for example inside here I could say something like well I tell you what grab the email which is currently going to be the same as user one so this email and set that equal to something else I'm gonna set it equal to this email and then just concatenate a little string which is gonna say cloned like that I'm also going to do a br tag right here just so if we echo out anything after this later on then it's going to go to the next line but anyway the point here is that we're taking the current email property which is going to be the same as user one which is this over here because that's what we're cloning it's going to take that property and it's going to add this bit of string to the end of it because when something's cloned this runs on the new instance the thing that we get back from that clone so if I save this and run it now we should see first of all this was removed because now we have two of those instances and those two are still removed but we don't see anything else because we've not actually echoed out anything else what I'm going to do now is in fact I'm not going to change this to email because that's a private property I'm going to say user name and this should be user name as well so we take the current user name and add cloned to the end of it now we can access that user name outside here so I'll say echo and then it's going to be user four and we want the user name property so the reason I changed it to user name was because this thing over here email was protected and I just wanted to quickly print out something to the screen so we went for the username instead so anyway now if I save this we should see Mario but we've cloned next to it so let me refresh and now we can see Mario cloned and also the user Mario cloned was removed so this is all working now so there's two magic methods for you we have this destruct magic method and also we have this clone magic method now there are also a load of other magic methods available to us in PHP and you can find them on this page right here so if you want to read more about one of these you can click on these and it is going to give you a detailed explanation and a lot of different examples as well on how to use those things so what I'll do is leave this link down below and you can check this out in your own time but now hopefully you understand what these two methods do

Original Description

---------------------------------------- More on magic methods - https://www.php.net/manual/en/language.oop5.magic.php 🐱‍💻 🐱‍💻 Course Links: Course files - https://github.com/iamshaunjp/object-oriented-php 🐱‍💻 🐱‍💻 Other Related Courses: + PHP Tutorial for Beginners - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gksOX3Kd9KPo-O68ncT05o
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →