How to create my own PHP magic method names? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Some frameworks have their own magic methods names, such as
$player->findByName('Lionel Messi')
which results in a simple SELECT * FROM players WHERE name='Lionel Messi' query. In PHP how can I make similar methods? Do they somehow catch the global MethodNotFoundException?

Use __call magic method.
Read more about it in docs, that is all you need.
http://php.net/manual/en/language.oop5.magic.php
public function __call($name, $args) {
// TODO: Parse called method name and run query if needed
}

Related

Object Declaration on a array variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
While Looking into a PHP Plugin i saw a line ,
$this->banks[0] = new Population();
It seems like they are declaring a object in a array variable. What is the use of it?
For what you described, it seems they're using a common pattern called Singleton that is useful in the way that you have all objects and their states accessible from only one common object.
It all depends on how the PHP plugin works.
The advantage of using a class instead of the array allows you to create function for a better data manipulation (e.g. a population class could have a function getPersonByName or getPersonsByAge which makes it easier instead of making a new loop each time.

Method Overriding vs Late Static Binding? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
When it comes with Inheritance, what Exactly is the difference between method overriding and late static binding?
Late static binding is essentially method overriding for static methods. There are some subtle differences in how they are actually carried out by the compiler. See What exactly are late static bindings in PHP?

What are the benefits of array type hinting in PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What might be some benefits of array type hinting in methods and functions in PHP besides documentation and readability?
Generally, the sooner you catch an error the easier it is to diagnose. Type hinging in php allows those errors to occur earlier than if something does wrong down in the function or in functions that it calls.

What is the prefered way of writing methods in Laravel? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Which one of these styles are prefered when retrieving data to display in a view?
Straight forward laravel with no custom methods in the model:
$guest->bookings->first()->id;
$guest->bookings->first()->bed;
$guest->bookings->first()->date;
or a method to get the latest booking model:
$guest->getLatestBooking()->id;
$guest->getLatestBooking()->bed;
$guest->getLatestBooking()->date;
or seperate methods for each value like this:
$guest->getLatestBookingId();
$guest->getLatestBookingBed();
$guest->getLatestBookingDate();
It depends if you need to attach other queries (like 'where', or 'select'). This doesn't mean that you always have to use the second option, third is excellent too. For the first one, try to avoid it.

Can I declare a global function from namespaced code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to avoid creating another file just to escape the namespace.
Found the solution. For others interested see below.
namespace { function myFunction() { ... } }

Categories