Presentación
Vídeo
Transcripción
Extracto de la transcripción automática del vídeo realizada por YouTube.
thank you can everyone hear me thanks to the collapse of the single clap whoever you work oh thank you sorry I didn't expect it but it was nice he needs okay so thank you Linkin My name is Matthew bar locker we're talking about magic methods today
show of hands who has heard of magic methods and PHP fantastic I hope I can deliver something you haven't heard though I quite expected the majority to not have heard it in either case well it will plow on so if you do forget my name you want to address
me by name just ask yourself who locked to the bar the bar Locker did hey awful awful joke terrible the sobering mentality my apologies I didn't realize that an introduction would be given so that's enough about main magic methods our special on PHP
there is actually a construct called a magic method that it's not something special about it that I made up or that I coined PHP has magic methods I first discovered magic methods completely by chance I didn't look for them I wasn't aware of them
what happened was I was using cakephp back in the day a long time ago and I had an issue with one of the models I was trying to do fine to buy ID in cake this allows you to specify an ID and it will get back to the database record for that ID for that primary
key and I was having an issue I couldn't figure it out with my code so i decided to go to the cake code and you know try and debug it a little bit more so I went to the model class in the cake core started looking did search for fine by ID found nothing
so I figured okay you know this is on object great I'll go to its parent did the same search found nothing and I went all the way up the stack to I think it's overloaded and found nothing and I was perplexed I was very upset that someone was playing
an awful trick on me and obviously they had SSH tin to my machine I changed the code and so I was upset having played a few pranks on other people and I had to be that after a little bit more research turns out a magic method was to blame in fact in this particular
case the magic method was underscore underscore call and this magic method as we'll talk about later allows you to have methods that are not explicitly on the object but that operate anyway and that was my problem every magic method in PHP begins with
underscore underscore some exists on classes there are underscore underscore functions that exists outside of classes so PHP recommends do not name any of your functions underscore underscore you don't prefix them with underscore underscore unless you
intend to override magic functionality making magic functions is very simple you just define them each one of them has a prototype that you must follow but if you just define them and follow the prototype you will be just fine for example i have a class here
and in this class i want to define the two string magic function it's as simple as public function to string then i return a string i have that magic function now every single one of these classes i'm sorry every one of these functions follows that
same pattern you define it and it works there are seven different types of magic methods there are many more just methods themselves but the types of them I'd like to talk about today I'd like to go over what they are what the specific functions are
how to call them what the prototypes are when to use them when other people have used them and different performance characteristics of them starting with string fication the only function the only magic method dealing with string vacation is to string I hope
that most of you are aware of this I'm going to cruise by it covering a key couple key points that will relate to the other ones but basically to string is used when you cast an object as a string if you echo an object it's turned into a string and
it gets printed out if you want to log an object turns it into a string prints it out you can use this function to turn anything into a string whether that be you know for your own debugging purposes or as I've said the logging or printing out there is
one caveat with the two string and that is that you cannot throw an exception in the middle of it if you do you'll get a fatal error it will be not so fun for you it does have to return a string in fact the third example no yeah third example I've
got the output of exactly what would happen if you didn't return a string so this first key point that I'm trying to show is that you need to define the function to get the magic qualities about it to get to the interoperability that a modification
of the cake core I'm sorry not cake the PHP core so in this first example I don't have a two-string method I didn't define 1 i'm going to try and print out this class anyway so at the top you see the class you see me instantiating class and
then echoing the class at the bottom here when i run the code it gives me a fatal error i cannot convert an object to a string without the two-string magic method i would get that error conversely if i did have a two-string method same class with a two-string
method i'm just going to print out the name of the class and then its constructor parameters I'm sorry not constructive parameters at its properties but a I run the class I'm sorry I run the code it prints out what is found in the two string function
[ ... ]
Nota: se han omitido las otras 2.619 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.