Object Declaration on a array variable [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 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.

Related

When to use PHP's variable variables? [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
I've been using PHP for some basics Back-End development for a while now. I saw something about interpretations of variables while I was looking for some changes which came with PHP 7. I'm not using them and it would be great if someone can explain why to use them?
What I mean is:
What are the pros of using them?
You use variable interpretation in situations when you need to dynamically reference a variable and don't want to use an array. Generally I would not recommend using it, as you lose benefits such as static code analysis.

What this sentence means? [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 7 years ago.
Improve this question
i have read this sentence in the object oriented paradigm approach.
A class can never have property values or state. Only objects can.
Can anybody elaborate this ?
Think of a class as a definition of a new data type. As such, it cannot have a state - it's just a definition. Consider this analogy - the concept of integer can't have a value, but a specific variable of integer type may have the value of 7.

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.

OOP PHP: Is it okay to store configuration strings in constants? [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
I am writing a class to process an associative array. Inside the class, array keys are used in getter/setter like functions.
Since I can't guarantee that the array keys will never change (either technical requirement or simply for convenience), is it okay to use class constants to store the array keys, so that the getter/setter like functions use these instead of a hardcoded array key?
That way, all array keys could be visible in one place in the class which would allow easy change/configuration.
It is a widespread and perfectly legitimate use of class constants.
So yes, it may "be ok".

Manage Huge PHP Class File [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 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()

Categories