All Frameworks Suck

41
So why do we still use them? MARTIN ZLÁMAL INDEPENDENT PHP DEVELOPER

Transcript of All Frameworks Suck

Page 1: All Frameworks Suck

ALL FRAMEWORKSSUCK So why do we still use them?

MARTIN ZLÁMALINDEPENDENT PHP DEVELOPER

Page 2: All Frameworks Suck

WHOAMI?Currently I am helping companies not to fall into shit (again).

First web-app ~10 years ago (total mess).

Now I am mastering Nette and I am in ❤ with PHP and JS.

Page 3: All Frameworks Suck

First you need to know this guy:

His name is Composer and he is awesome.

Page 4: All Frameworks Suck

Working with composer is very easy

Pros

ConsI don't really know

SemVer

Dependencies

$ composer require <vendor>/<package>

Page 5: All Frameworks Suck

Now, Let's be

honestBrutally

Page 6: All Frameworks Suck

suckAll

Frameworks

Not very optimistic, huh?

Page 7: All Frameworks Suck

Frameworks are solvingvery specific problemin very general way

But it's actually your problem

And at this moment it solves these problems way betterthan you are able to do. I am sorry...

Page 8: All Frameworks Suck

MVPMore Vicodin PillsMost Valuable PlayerModel View Presenter

Page 9: All Frameworks Suck

It's basically aboutseparation of concerns

Please forget MVC, MVP, MVx...

It's up to you how you'll mess up...

Page 10: All Frameworks Suck

Stick with these rules:

rule #1

rule #2

rule #3

You don't want a lot of logic in the templates

You don't want a lot of logic in the controllers

You want to have a lot of business logic in themodel layer with a lot of tests...

Page 11: All Frameworks Suck

Tests? What do I mean with tests?$ composer require nette/tester

Page 12: All Frameworks Suck

Pleaseremember!Model is not only about databasemanipulation!

This is where the business logic lives andprospers.

Page 13: All Frameworks Suck

Usually modelconsists ofDomain logic

Implementation details

Domain access points

It doesn't know about database at all

Acts like a gateway to the domain logic

This is about how it's going to be saved or exported

Page 14: All Frameworks Suck

I'll show you one extremelyimportant design pattern

Dependencyinjection

Page 15: All Frameworks Suck

class Document { String html;

Document(String url) { HtmlClient client = new HtmlClient(); html = client.get(url); }}

The Clean Code Talks - Don'tLook For Things!

What is wrong?

https://youtu.be/RlfLCWKxHJ0

Page 16: All Frameworks Suck

class Document { String html;

Document(HtmlClient client, String url) { html = client.get(url); }}

There is no instantiationJust description of what you need

This example is still not good enough. Please look at the video tounderstand basic principles of testable clean code...

Page 17: All Frameworks Suck

There is Nette\DI for that

It's usually configured via NEON

ne-on.org

"DI container is the supreme architect which can createindividual objects (in the terminology DI called services) andassemble and configure them exactly according to our needs."

Page 18: All Frameworks Suck

Let's go backward

"PHP wasn't supposed to be a language on it's own"

"It was just an interface or gateway to the business

logic that was written in the strongly typed compiled

language." (C)

"But the web grew so fast..."

(1994)

Page 19: All Frameworks Suck

"Everyone just started using the templating system

entirely to write all their business logic."

"That's kinda what happend..."Source: PHP Frameworks Day 2013 ­ Rasmus Lerdorf

Yeah.We are using templating languageto write templating languages.

Fuck.

Page 20: All Frameworks Suck

Let's go forward(2010)

Facebook introduced XHP: A New Way to Write PHP

if ($_POST['name']) {

?>

Hello, <?=$_POST['name']?>.

<?php

} else {

?>

<form method="post">

What is your name?

<input type="text" name="name">

<input type="submit">

</form>

}

if ($_POST['name']) {

echo Hello, {$_POST['name']};

} else {

echo

<form method="post">

What is your name?

<input type="text" name="name" />

<input type="submit" />

</form>;

}

Page 21: All Frameworks Suck

XHP is sort of templating languageFacebook introduced XHP: A New Way to Write PHPBut why? Why is it so important?

Ok, let's try punish support.zcu.cz

Still don't understand why is it soimportant? Ok, let's steal my identity...

Page 22: All Frameworks Suck

$ composer require

latte/latte

How to find a good templating engine?

Speed? Nah, not so important. It's going to be compiled anyway...Security? Hell yeah!Syntax? Yes - ask your coders.

Ok, what about speed, security and syntax in Latte?

Page 23: All Frameworks Suck

Speed of Latte

This nonsensical graph proves

my point that Latte engine is

fast. Almost every templating

engine is compiling templates

into cache (once). There is

pure PHP in the cache.

Therefore speed is not an issue...

Page 24: All Frameworks Suck

Security of LatteContext-Aware Escaping FTW!

<p onclick="alert({$movie})">{$movie}</p>

<script>var movie = {$movie};</script>

<p onclick="alert(&quot;Amarcord &amp; 8

1\/2&quot;)">Amarcord &amp; 8 1/2</p>

<script>var movie = "Amarcord & 8 1\/2";</script>

$movie = 'Amarcord & 8 1/2';

Page 25: All Frameworks Suck

Syntax of Latte

{if $cond} ... {/if}

{foreach $array as $key => $value}

<ul n:if="$items">

<a n:href="Homepage:default">

<h1>{$heading|lower|capitalize}</h1>

<h1>{$heading|truncate:20,''}</h1>

Page 26: All Frameworks Suck

Now the controllersIt is always about recieving request and creating response

Page 27: All Frameworks Suck

Nette\Application\IPresenterIt works just like this:

IPresenter#run( AppRequest ): IResponse

IRouter#match( HttpRequest ): AppRequest

Page 28: All Frameworks Suck

Nette\Application\UI\PresenterUpgraded with lifecycle (simplified):

UI\Presenter#renderShow(): void

UI\Presenter#actionShow(): void

UI\Presenter#handleSignal(): void

Page 29: All Frameworks Suck

Oh, hey - do you know Tracy?She is so sexy...

Page 30: All Frameworks Suck
Page 31: All Frameworks Suck
Page 32: All Frameworks Suck

github.com/mrtnzlml/kiv-web-2016-11-30

Doctrine 2MVPLatteNette DI

Symfony ConsoleUUIDTracyPHP 5.6+!

Here you go

Form + LoginNEONIdRouter

Page 33: All Frameworks Suck

Do not! #1

@(Based on your GitHub repo)

This is called "shut the fuck up" operator

Page 34: All Frameworks Suck

Do not! #2

if ($page == "")

$page = "uvod";

Technically it's all right, but there are two problems...

Page 35: All Frameworks Suck

Do not! #3echo "<html>";

echo "<head>";

echo "</head>";

echo "<body>";

echo "<h1>Moje aplikace</h1>";

// ...

There is no reason for that.

Page 36: All Frameworks Suck

Do not! #4class app

{

public function app() // this is constructor :(

{

// this is hidden dependency

$this->db = new db();

}

// ...

Also, it's deprecated in PHP 7+

Page 37: All Frameworks Suck

Do not! #5

global $variable

$GLOBALS['variable']

// + pseudoglobals

This is just OMG, FFUUU!

Page 38: All Frameworks Suck

Avoid bad practices*.class.phpfile name !== class name (case sensitive)Engliczechich language v kódudirect superglobal accessPDO::query without bindingdon't worry about stdClass

Think before you learn!

Why? Almost everything is class...

PSR-4?

Page 39: All Frameworks Suck

Use actual PHPPHP 5.6 is going to die in 3... 2... 1...

Actual version is PHP 7.0.12

PHP 7.1 should go out tomorrow

(until Dec 2017)

31 Dec 2016 (1 month!)

Be reasonable!

Page 40: All Frameworks Suck

And yes, I am sorry...

Page 41: All Frameworks Suck

THANK YOU FOR LISTENINGI will now answer any questions you may have

Please visit zlml.czFollow me if you want @mrtnzlml