Manage Huge PHP Class File [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
I have a huge php class with very long code in it. This make the file is big with lots of coding and hard to maintain. Is there anyway that I can split the class into a few files for easier maintaining?
Thank you.

It depends on whether or not you can split the class in to subclasses.
What I mean is that you should ask yourself "does this method apply to this class or would it belong to a subclass?"
For example,
Class Animal
- dog_bark()
dog_bark() could be moved to a class named Dog, and the method renamed to bark()

Related

Any difference between this 2 ways to use a class in PHP? [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 last year.
Improve this question
Currently developing a Laravel app and a question came to mind.
Is any "technical" difference between this two ways to use a class?
First way:
public function store() {
new \Illuminate\Auth\Events\Registered($user)
}
VS
Second way:
use Illuminate\Auth\Events\Registered;
public function store() {
new Registered($user)
}
One way is more performant than the other?
Is any difference in the way PHP reads this lines?
When the class is only used once, is better to include it using first way or second way?
Also any other this than comes to mind...
No difference in performance but its personal choice but if you are using the class more than once in the same class importing once works well and looks clean in my view

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.

Is creating a constant or enum class for template page efficient? [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 7 years ago.
Improve this question
Basically I'm building a website that need a html like template called from the folder using their file name.
Should I use a enum/constant class or should I just call them straight to their filename?
Example:
abstract class ViewTemplate
{
const ORDER_HISTORY = 'order_history_template.html';
const PURCHASE_HISTORY = 'purchase_history_template.html';
//etc...
}
Is this kind of class necessary and do they hog a lot of resources if I call the class when I have a lot of template?
Since the filename is a unique identifier - unless you expect the file names to change in the future (which isn't likely, and even if it happens refactoring is simple) there is no point to it.
You already have a unique identifier for a file you're loading. The only benefit of passing an enumeration with properties here is type-safety but you're only calling it once anyway so a typo could happen in one place anyway.
So overall it's redundant.

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.

How to check if not yet autoloaded class exists [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
In Symfony2 I want to check if a class file can be found before using it (meaning before autoloading it)
Is there a Symfony component to test whether the class file exists in one of the class paths?
Use class_exists(). The function's second argument specifies whether autoloader should be called or not if the class hasn't been defined.
You can use Symfonys class loader. Like this:
if(!$loader->findFile("\Namespace\Sub\Class")) {
echo "file not found";
}

Categories