Tag Info

New answers tagged

0

Yes, that is the template method pattern Currently there is no need to make the getType methods public, "protected" would be enough. If you actually need them outside of that class hierarchy, you can make them public, of course.


0

The best way I have found to do this is as follows: class This_Is_My_Class { use My_Trait; protected $prefix = ''; function __construct() { $this->prefix = 'not_empty'; } protected function get_prefix() { if ( empty($this->prefix) ) { throw new Exception("Prefix must be non empty."); } ...


2

One reason for introducing a separate variable is to improve readability, assumed the real name of $someClass->thisVar() is not expressive enough. This can make sense even if the function is only called once: function someFunction(SomeClass $someClass) { $explainingName = $someClass->thisVar(); doSomethingElse($explainingName); } Of course, ...


2

I would solve this with three tables: products, features, and product_features. Products id name description 1223 A abcdefg 1224 B hijklmnop 1225 C qrstuvwxyz features id name description 20 stages such-and-such 21 pipes 22 holder 23 orientation product_features PRIMARY ...


1

I don't know if this is what you're looking for but here goes. Methods like save, get, getBy, getAll, etc, are generic and would ideally work across multiple models. For the most part, the only difference is going to be SPECIFICALLY where that data is. So why not make a super model of sorts for all your models to extend. class Super_Model { public ...


0

My thought is that it is correct and usable, but not necessarily good practice. My understanding of an interface is to promote (among many things) SoC, not to store credentials for a specific database. Intermingling connection information with the interface limits the use and purpose of the interface.


0

you need to look at something called cron jobs. Cron jobs execute a job at a specified interval. In this case, a php script. Check these resources for more information on cron jobs: http://en.wikipedia.org/wiki/Cron and for cpanel: https://my.bluehost.com/cgi/help/cron Let's say you have a script called cronJob.php and a second called sendFunction.php ...


4

Check before, please. Which would you rather read and debug, after the code has been modified a few times. This? function someFunction() { if($condition1) { if($condition2) { if($condition3) { //Execute code } else { throw new Exception("Cant execute code because the third condition is ...


0

Unless we have one of the core PHP developers here, the best you're likely to get is a quote from Eric Lippert's stock answer to this kind of question: "Features cost money, time and effort, and take away money, time and effort from features that would benefit the user better; features have to be justified based on a cost-vs-benefits analysis. " This ...


0

I would say it depends on your needs. Do you need to know the name of the variable? Do you want the (multiple) variables to be passed in a specific order? Is the amount of variables going to grow (like filters)? Does it need to be part of the url? (i.e. for SEO reasons) I mostly know two variants (different then the examples you gave), and in ...


2

If a method uses $this it cannot be called statically. Or at least it shouldn't be called statically even if some sort of weird PHP behavior allows it. You never know with PHP. If you insist on keeping your architecture as is, i.e. with monsters drawing themselves (which is not a good architecture, like Doval said), the simplest way would be something like ...


0

The web server sees http requests not any of the dns voodoo involved so it is just a name based virual host like the examples provided at http://httpd.apache.org/docs/2.2/vhosts/examples.html


1

Somewhere in your code you have to store the specifics of each product. In your example, the natural place for this seems to be the Specs class, since I guess it is something like the "configuration" of your library. I would extend that class, for example, by an attribute "calcFactor", so your calcVar function will look like this: public function calcVar() ...


3

I honestly think you need to give up on not distributing code in your case. Closed source does not mean you don't/can't distribute code, it means that they are not allowed to distribute it. I think you should really evaluate your goals. If you want to stop people from looking at your code, then use an obfuscateor, if you want to bill per install then use a ...


1

You can use this structure too... Products: -------- ProductID | CategoryID | Title | Cost | TimeStamp 1 | 1 | Audi | $0 | ----- Specifications -------------- SpecificationID | Specification 1 | Model 2 | Fuel 3 | HardDisk ProductSpecifications: ---------------------- ProductSpecificationID | ...


1

I'd wondered as well. Googling a lot I found the answer posted by Benjamin Eberlei himself (Doctrine Project Lead and Contributor): Hello, of course DBAL uses PDO internally and escapes parameters such as in your example. I think the docs are messed up here. The Paragraph relates to the two subparagraphs about quote() and quoteIdentifier() ...


1

Personally I would be using factory methods to create each of these objects and would forget about the additional inheritance you have designed. The form of this would be a ReportFactory class and a SystemFactory class that would have their own creation methods. In the creation methods you would instantiate the required class, and then perform the ...


3

This isn't an answer to your question, but a solution to your problem. The issue here is that $Monster->drawSprite() is a non-starter; having monsters know how to draw themselves is a violation of the Single Responsibility Principle. For that same reason it doesn't make sense to try to make a method pull "double duty" by being both an instance method and ...


2

While views shouldn't contain business logic, they still can contain basic conditional logic, loops, etc. Since your website is “very heavily based on whether an event has occurred or will occur”, this is definitively a case where you'll have two views, not one. What questions could I ask myself to help me determine the best approach to take to this in ...


1

I've never used Laravel, so I'm not 100% sure this will work for you, but one method I've used in the past is to include either a list or an optional value in my viewmodel that contains the name of another model to render and the data to put there. The view then iterates over the list/checks for presence of the value and includes the appropriate view ...


0

What about using Zend Guard or ionCube PHP Encoder? Or why is it a problem to distribute your source code for a fee? This is a problem not special to PHP. Many companies write libraries that they sell in source code form.


3

Describe the purpose and usage of the interface, and the purpose and usage of the functions in the interface. Here is an example from the .NET Framework (simplified for brevity). Note the "statement of purpose," and the specific notes about jitting and internal hacks, things which would not at all be obvious just by looking at the interface code: // ...


0

You should use PDO (PHP Data Objects) for preventing SQL Injection attacks. It supports 12 different database systems. Your Generic Class for connecting to MySQL using pdo would look like this: class Db{ public function dbConnect(){ try { $conn = new PDO('mysql:host=host_name/ip;dbname=database_name','user','password'); ...


0

In this situation, I always think of the interface first, then write PHP code to support it. It's a REST API, so meaningful HTTP status codes are a must. You want consistent, flexible data structures being sent to and from the client. Let's think of all the things that could go wrong and their HTTP status codes: The server throws an error (500) ...


-2

I was facing something similar, I did 3 things, Created a ExceptionHandler for myself called ABCException. Since I am using Java & Spring, I defined it as public class ABCException extends Exception { private String errorMessage; private HttpStatus statusCode; public ABCException(String errorMessage,HttpStatus statusCode){ ...


1

You might consider managing your frontend dependencies with some dedicated frontend tools. A common combination is using Grunt and Bower to manage your javascript and CSS files. A dedicated setup like this makes it easy to do things like define different tasks for development and production, so while developing you include as many scripts and styles as you ...


1

As GordonM mentioned in the comment, you should break it up to something along the lines of \Vendor\Package\ClassName So maybe for your cache library it would be something along the lines of \SG\Cache\CacheClass Directory structures usually mirror the above. So it'd be located in /SG/Cache/CacheClass.php of your root directory. If you're using a ...


-2

I personally find camelcase to be more natural when coding variables, but because variables in PHP are case sensitive, and it's no fun hunting through code looking for a variable you forgot to make camelcase, I always use snake case for variables. While its true that the conventions are mutable, coding after someone that has used camelcase on variables, ...


1

Implicit dependencies are bad, especially if your template engine will not complain about missing variables. I believe that it would be beneficial for each template fragment to know which variables it needs, and to refuse to be rendered or instantiated with wrong arguments. Here is an example where the lack of scoping will introduce difficulties: ...


1

There is not such thing as only enterprise web application. In the enterprise world you typically have a full enterprise system, where the web part is pretty small compared to the whole system. There are many systems underneath the web that perform batch processing, queue messaging, enterprise integration, gateways and many more. Event at the top there might ...


6

Unfortunately, this is just how email works. There is no way for PHP to validate whether you are allowed to send mail from a particular address, so it will let you specify any address you want. Your mail server may be configured to reject email from invalid mailboxes, or if you do not have the correct authentication details for a valid mailbox, but most ...


0

Using a PHP MVC framework for ios development is a good idea but you should definitely take a look at these Moblile App development frameworks.They additionally provide access to back-end API's & you can run tests in php,python,ruby etc.(Mostly they work both for ios & android apps). And if its only that you want to use a php framework then take a ...


2

You could create a third class that manages both the repository and the cache. Then, don't manually try to get the cached value and if not available query the repository like $activity = $this->cacheHelper->getFromCache($key, $name, "ArrayCollection<DW\DWBundle\Entity\Activity>"); if ($activity == null) { $activity = ...


1

Unfortunately, the "best" way of sending out emails of this nature is going to vary depending on the specifics of your project. There isn't a one-size-fits-all solution. If your user-base isn't very large, rolling your own solution can be a quick and easy way to get the job done. However, as your user base increases, so do the resources necessary to handle ...


1

No Composer is a dependency manager for PHP only while Bower is optimized for front end dependencies that can be used with not only sites built with PHP but built with other programming languages and frameworks. I would stick with Bower for front end dependency management as that's what its designed for. :)



Top 50 recent answers are included