News for hackers. Stuff that matters.
Re:PHP 7 Released by nnq on Wednesday December 02, 2015 @07:54AM (#10662620)
> I urged him to switch to a sane framework like Laravel

...why didn't you taught him about PDO, so he can start with something more universal, not tied to one particular framework that happens to be popular nowadays?!

Then he can learn whichever OR/DM or DAL he might like need (Eloquent, Doctrine, Redbean, IdiORM/Paris etc.), and also be able to pop the hood and debug it when needed.

I never get it why so many otherwise-good PHP devs have their heads full of framework-specific knowledge and have a propensity to fall in love with heavy frameworks like Laravel, or worse, with leviathans like Symphony or Zend ...instead of playing to the language's advantages and using light-weight tools and libraries that would allow them to move faster.

Re:PHP 7 Released by trm42 on Wednesday December 02, 2015 @08:34AM (#10662807)
I could've done that but that wouldn't have taught the same things. PDO is awesome but requires deeper level of understanding (for a beginner), which may increase the frustration and may end up returning back to the mysql_query and co.

It's easier point to framework that's already using PDO and and making sane defaults and solving bootstrapping problems. Once you've learned bunch of new concepts like ORM in one framework, it's easy to understand other ORs and be able to compare it to DMs.

My own experience with Laravel has been really positive. Actually I was thinking about creating something similar until I learned about Laravel, so it solved lots of existing annoyances regarding PHP coding for me.

Everybody needs to start somewhere and I believe that by nudging towards Laravel, my young friend'll learn more in the long run and in the meantime can concentrate on making services instead of writing home grown PDO helpers and custom routing libraries. They are problems that have been solved already multiple times.

Also, Symfony and Laravel communities are encouraging and teaching about the better ways of doing things so that'll help him in the long run.

In many small project it's more fun and more easier to use $favorite_framework instead of searching for reinvented wheel. There are cases when it's better to do more custom solutions but for many cases frameworks are just awesome

Re:PHP 7 Released by sarciszewski on Wednesday December 02, 2015 @09:02AM (#10662935)
> PDO is awesome but requires deeper level of understanding (for a beginner), which may increase the frustration and may end up returning back to the mysql_query and co.

Yeah, that's basically why I wrote EasyDB. https://github.com/paragonie/easydb

    $rows = $db->run('SELECT * FROM comments WHERE blogpostid = ? ORDER BY created ASC', $_GET['blogpostid']);
    foreach ($rows as $row) {
        // etc
    }
Teach people to do things this way rather than concatenate strings, say goodbye to SQL injection vulnerabilities.
Re:PHP 7 Released by wmil on Wednesday December 02, 2015 @01:16PM (#10664707)
At the same time I understand why it would be more confusing for newbies.

In a language that already has string interpolation you're telling them to use a crappier custom version of string interpolation that's safe for databases.

Tutorials need to be more upfront about that.

Re:PHP 7 Released by sopooneo on Wednesday December 02, 2015 @02:23PM (#10665183)
But if the example above uses PDO underneath, then it's not just string interpolation though I think. It's sending the query and the parameters separately to the database, which is creating a "prepared data object" to plug the parameter values into.

I may be wrong, so please correct me if so.

Re:PHP 7 Released by sarciszewski on Wednesday December 02, 2015 @01:41PM (#10664875)
I've got "writing an open access PHP 7 online book to hopefully serve as a new, best-practices tutorial" next on my to do list.
Re:PHP 7 Released by bpicolo on Wednesday December 02, 2015 @12:16PM (#10664239)
Yep. PDO still leaves room for string concatenation issues. Unfortunately, so does Doctrine.
Re:PHP 7 Released by EGreg on Wednesday December 02, 2015 @02:07PM (#10665069)
Do you know any good PHP database frameworks that help you build up the query programmatically?

Over the years, we built one in-house at http://qbix.com/platform/guide/database because we couldn't find one. It does things like sharding out of the box, because it's able to understand the query's criteria and target it to the right shards.

Re:PHP 7 Released by trm42 on Wednesday December 02, 2015 @04:00PM (#10665767)
Laravel at least has Query Builder: http://laravel.com/docs/5.1/queries

As an interesting side note: Laravel's AR ORM (=Eloquent) is built on top of the Query Builder so you can leverage Query Builder with AR if you know what you're doing. Can also lead to big problems if you don't realize the possibilities and the caveats.

Re:PHP 7 Released by EGreg on Wednesday December 02, 2015 @05:42PM (#10666415)
Like what kind of problems?
Re:PHP 7 Released by trm42 on Thursday December 03, 2015 @04:26AM (#10668678)
Here has been discussion in another comments about overusing ARs for trying to achieve too complicated things that'll lead to DB performance problems. As stated elsewhere here, AR is good for CRUD operations but after that you should use Query Builder or plain SQL to achieve the more complicated cases.
Re:PHP 7 Released by sarciszewski on Wednesday December 02, 2015 @12:26PM (#10664318)
People who want to do string concatenation will continue to do so no matter what we tell them. At some point, we have to educate users. Giving them an easy-to-use alternative is a step in the right direction.
Re:PHP 7 Released by mikeash on Wednesday December 02, 2015 @12:42PM (#10664467)
Why not have the obvious API call for making a query only accept constant strings? You can still have an escape hatch for the rare cases where an expert needs to do something fancy, but hide it well and make it scary and you shouldn't have many problems.
Re:PHP 7 Released by sarciszewski on Wednesday December 02, 2015 @12:46PM (#10664499)
Right, well, "constant strings" don't really exist in scripting languages, and before you mention objects that encapsulate them, what's to stop developers from building them elsewhere before passing the string to an object that encapsulates them?

Chicken and egg.

Education is the security strategy that pays forward the most.

Re:PHP 7 Released by mikeash on Wednesday December 02, 2015 @12:55PM (#10664563)
Apologies, I meant string literals.

There's no technical reason the language couldn't make it so that string literals can be identified at runtime.

There are several good ways to put a stop to SQL injection. Better education is one, better APIs is another. There is no reason to just give up on the idea of using SQL queries directly because of injection attacks.

Re:PHP 7 Released by sarciszewski on Wednesday December 02, 2015 @01:03PM (#10664615)
Pop quiz: What is the singular cause of SQL injection, XSS, and stack overflows that causes a security vulnerability?

...

The answer is: Data being treated as an instruction.

Solution: Separate them so that data can never be interpreted as an instruction!

In SQLi, this solution is to use parameterized queries. You send the query in one packet, then the parameters in a second one. SQLi is thus prevented.

(Not that SQLi is the only vulnerability possible.)

Re:PHP 7 Released by mikeash on Wednesday December 02, 2015 @01:45PM (#10664905)
I'm not sure what your point is here, because what I'm advocating is precisely to make it easier to use parameterized queries and more difficult not to.
Re:PHP 7 Released by sarciszewski on Wednesday December 02, 2015 @02:03PM (#10665043)
I wasn't being argumentative, I just thought that would be something worthwhile to add for people following along.
Re:PHP 7 Released by mikeash on Wednesday December 02, 2015 @02:10PM (#10665089)
Gotcha, sorry. "Pop quiz" always makes me think it's being sarcastic.

So, yes, total agreement there. Parameterized queries are key. I find it crazy that anything else ever existed, let alone still gets used.

Re:PHP 7 Released by sarciszewski on Wednesday December 02, 2015 @03:22PM (#10665536)
Ah, yeah, I hadn't considered the normal usage.

Most people don't realize how much these vulnerabilities have in common, in the abstract, until you frame it like that.

Re:PHP 7 Released by Iridiumkoivu on Wednesday December 02, 2015 @12:23PM (#10664294)
What do you think of Propel?
Re:PHP 7 Released by reitanqild on Wednesday December 02, 2015 @08:59AM (#10662923)
Agree, then again there are different types of people.

Some wants to learn from bottom up(analytic approach), others want to see cool stuff then tweak, modify, replace to find the limits (holistic approach).

IIRC there is a tendency that young people prefer the holistic approach while teachers either are selected for or grow into preferring the analytical approach.

Re:PHP 7 Released by trm42 on Wednesday December 02, 2015 @09:21AM (#10663039)
Really good point! Most of us veterans assume that because we have learned things the hard way and from bottom up, everybody else should learn like that.

"Make them suffer like us and they will learn the holy truth" ;D

Re:PHP 7 Released by ludamad on Wednesday December 02, 2015 @11:45AM (#10664004)
Whenever I know something well I feel like I'm excited to teach people 'just the good techniques'. I do notice that the things I describe don't sound as cool because they haven't struggled with the less cool stuff, though
Re:PHP 7 Released by cpplinuxdude on Wednesday December 02, 2015 @12:34PM (#10664407)
Doesn't holistic approach just make sense?

It's not like you learned to speak with your parents reading you the dictionary beginning with the letter A.

Re:PHP 7 Released by datasage on Wednesday December 02, 2015 @01:33PM (#10664827)
Its a good way to start and actually make things happen. But it's also important to get beyond that to become a strong developer.

Some do, some don't.

jQuery is a common way to learn to do front end programming. It saves a lot of time when used well. But it is important to extend the knowledge to core javascript and be able to write code when jQuery is not available (like when writing scripts to embed on different sites).

Re:PHP 7 Released by steveklabnik on Wednesday December 02, 2015 @02:36PM (#10665265)
The approaches aren't exclusive either, you can start "holistic" and then learn more by going "analytic" afterwards, once you have the gist of things.
Re:PHP 7 Released by reitanqild on Wednesday December 02, 2015 @03:32PM (#10665592)
Agreed, this is my preferred way of learning.
Re:PHP 7 Released by mzarate06 on Wednesday December 02, 2015 @12:43PM (#10664468)
> Laravel ... solved lots of existing annoyances regarding PHP coding for me.

Can you name a few such annoyances and how Laravel addressed them?

And do you mean to say that Laravel addresses them, while other frameworks don't?

Re:PHP 7 Released by aarondf on Wednesday December 02, 2015 @09:56AM (#10663257)
> instead of playing to the language's advantages and using light-weight tools and libraries that would allow them to move faster.

Depending on what you mean by "move faster", I may have to disagree. Besides a one-off script, I can't envision a scenario where using "tools and libraries" would allow you to move faster than using Laravel.

Laravel comes with an unbelievable amount of stuff done for you, right out of the box. Routing, Auth, Database, Queueing, Caching, Asset Pipeline, Templating, etc, etc. If your goal is to rapidly develop an application, there really is no better framework than Laravel to get that going. There's even a vagrant box that's all tooled for Laravel.

TBF, I may have misunderstood what you meant by "move faster".

Re:PHP 7 Released by brightball on Wednesday December 02, 2015 @10:18AM (#10663408)
Laravel is an excellent Rails clone, but the entire problem with Laravel is that it's a PHP framework.

I've been a PHP dev for 15 years and the biggest issue with frameworks in PHP is PHP itself. A request comes in, the PHP process loads up everything, executes the request and tears down...on every single request.

In just about every other language, the application boots up, loads everything into RAM and each individual request doesn't have to reload things.

There are definitely trade offs here. You can have a 1 TB hard drive filled up with PHP code on a 256mb of RAM server and everything is still going to run fine. There's a lot of value in that and most people who are PHP averse seem to be turning to Lamba from AWS to fill that gap. PHP scales down better than just about anything else out there.

When the other commenter mentioned doing lightweight scripts with PDO that's essentially what he meant though. Lightweight avoids reloading libraries and PHP is lightning fast when you do that.

A lot of progress has been made over the years to help PHP handle frameworks better, but there's a big reason why PHP grinds to a halt with heavy frameworks and that's it. Check out the techempower framework benchmarks.

Using PHP like Go, however, and just pulling in what you need can be extremely powerful.

Here's an entire presentation on it that I gave to a local PHP group. http://www.slideshare.net/barrywjones3/whats-the-right-php

FWIW, I do really like Laravel and even recommend it if you HAVE to use a big framework.

Re:PHP 7 Released by nivla on Wednesday December 02, 2015 @10:50AM (#10663621)
>A request comes in, the PHP process loads up everything, executes the request and tears down...on every single request.

Wouldn't that make debugging much much easier? Since you get a clean slate for every request, bugs can now be tracked and replicated in a saner way. I personally prefer this method for building and maintaining a huge and complex application despite knowing that it may not be an efficient one.

Re:PHP 7 Released by brightball on Wednesday December 02, 2015 @10:58AM (#10663687)
It does, but you get that whether or not you're using a heavy framework.

Without a heavy framework, you get the best of both worlds.

Re:PHP 7 Released by alphadevx on Wednesday December 02, 2015 @10:31AM (#10663509)
> A request comes in, the PHP process loads up everything, executes the request and tears down...on every single request.

It's called the "shared nothing architecture", and its by design. Nothing is shared between requests, so why _not_ tear down?

That model actually has a lot of benefits for web applications (it scales very well), and I consider it one of PHP's strong points.

Re:PHP 7 Released by brightball on Wednesday December 02, 2015 @10:41AM (#10663569)
I do to, except with heavy frameworks.
Re:PHP 7 Released by alphadevx on Wednesday December 02, 2015 @10:55AM (#10663664)
Yep you're right, its the framework bootstrapping that's the killer, especially when those frameworks are expensively parsing config files each time (Yaml, XML...).
Re:PHP 7 Released by spdionis on Wednesday December 02, 2015 @12:44PM (#10664487)
Which modern frameworks never do. The parsed data is usually cached in a php file. Then the (compiled to bytecode) files are cached in opcache. The interpreter doesn't even hit the filesystem on every request.
Re:PHP 7 Released by EGreg on Wednesday December 02, 2015 @02:08PM (#10665081)
With caching, this is actually not such a big problem. The isolation is very good for resilience. Compare, for example, Node.js where a single exception can bring down your entire process and all the requests it's currently executing.

Is Laravel really a Rails clone? I thought that was Symfony. I honestly don't understand why the most popular PHP frameworks are all Rails clones. In my own work, I made things more in a PHP style than Ruby.

Re:PHP 7 Released by brightball on Wednesday December 02, 2015 @04:29PM (#10665977)
That's not a bad thing fwiw. Rails is popular for a reason.
Re:PHP 7 Released by adrianmacneil on Wednesday December 02, 2015 @10:26AM (#10663464)
Pretty sure newer versions of php have an opcode cache built in, so they don't need to load everything on every request now?
Re:PHP 7 Released by brightball on Wednesday December 02, 2015 @10:28AM (#10663483)
Unless something has changed, that's the equivalent of keeping the files in RAM but it doesn't maintain state and routes, etc so it still has to reprocess.
Re:PHP 7 Released by acveilleux on Wednesday December 02, 2015 @10:52AM (#10663635)
That depends on implementation. The cache could be maintained in shared memory, an external process or disk files and in all cases can provide a significant speed up over reading in and compiling hundreds of files without burdening all workers with all compiled opcodes.

The current implementation uses shared memory.

In addition, that does not mean retaining all the variables and data, only the compiled opcodes and that's the bulk of the startup time.

Re:PHP 7 Released by Zelphyr on Wednesday December 02, 2015 @10:17AM (#10663406)
Until you hit something the framework wasn't design to do (which happens 100% of the time in my experience). Now you're working for the framework instead of it working for you.

This guy said it better: “Frameworks invert control. They control the lifecycle of the app, and give you entry points where your code runs. You’re still responsible for the final code, but you’re not in control.” https://aerotwist.com/blog/the-cost-of-frameworks/

Re:PHP 7 Released by 27182818284 on Wednesday December 02, 2015 @10:56AM (#10663672)
I find this very true from my personal experience, but I also fine the repetition in my work to happen often enough that if you don't use a framework, you end up making your own framework
Re:PHP 7 Released by eropple on Wednesday December 02, 2015 @10:25AM (#10663460)
Depends on the framework. Dropwizard, for example, is built upon Jersey, which is designed to be modular and to allow components, even in core, to be extended or replaced (because they're nothing-special Java objects, all the way down). You have to understand the framework, but it's generally safe to assume that the intentions of the framework are good and that by adhering to the contracts between objects you'll get the result you want.
Re:PHP 7 Released by acomjean on Wednesday December 02, 2015 @11:14AM (#10663803)
I'm not a big fan or frameworks for that reason. I do like some of the framework functionality and my apps are relatively small so I ended up going the micro framework route (Silex), using what I want (routing, forms, twig) and basically building the rest of the application in my own way.
Re:PHP 7 Released by lostsock on Wednesday December 02, 2015 @07:27PM (#10666928)
Can you give an example of something you would like to do that a framework like Laravel won't let you do or you feel like you have to fight against?
Re:PHP 7 Released by treeface on Thursday December 03, 2015 @06:54PM (#10673642)
Yeah, I hear that argument a lot from people who don't understand how the service container works. Laravel is really quite easy to understand if you have a decent knowledge of PHP. You can replace entire chunks of the framework for basically no cost if you want something to work differently.