I understand how to create a class, instantiate it, as well as access properties and methods, but $myobject->function() is about as complicated as it gets for me for now.
What is this structure?
$myobject->function()->something. I am repeatedly seeing this more and more, especially as I start wrapping my head around PDO queries. For example:
$query->function1($arg)
->function2($arg)
->function3($arg);
What’s happening here? is it simply chaining one call of multiple methods in the class, or are those sub-functions of function1()? What would the class definition look like?
$myobject->function()is returning an object,->somethingwould access a field on that object.Basically, the main concept in this programming method is that an object contains data and methods (or “functions”) to interact with the contained data. These containers can be conveniently passed around. PHP is giving you some shorthand so you don’t have to assign everything to a variable.
Returning objects is a way to layer functionality, so your PDO object, which has a query method, may return a row object. The row object has its own set of methods (e.g. for getting column values), so you can then invoke those methods.
So:
Is really just shorthand for: