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";
}
Related
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.
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'm using CakePHP (v3.0) and I want to create a bar chart.
I have tried to use "libchart" and "pchart" libraries but always I get problems (I think that namespaces is one of them..) to import and use these libraries on my controller class...
What's the simplest way to create a bar chart on CakePHP 3 ?
You can place the chart libraries in vendor folder and include them using PHP's include function.
To call their class just use a backslash before them to make them a Fully qualified name like
$chart = new \VerticalBarChart(500, 250);
Now you can use $chart without any issues.
References:
PHP Namespaces
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.
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
}
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()