All Frameworks Suck

Post on 19-Jan-2017

361 views 0 download

Transcript of All Frameworks Suck

ALL FRAMEWORKSSUCK So why do we still use them?

MARTIN ZLÁMALINDEPENDENT PHP DEVELOPER

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.

First you need to know this guy:

His name is Composer and he is awesome.

Working with composer is very easy

Pros

ConsI don't really know

SemVer

Dependencies

$ composer require <vendor>/<package>

Now, Let's be

honestBrutally

suckAll

Frameworks

Not very optimistic, huh?

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...

MVPMore Vicodin PillsMost Valuable PlayerModel View Presenter

It's basically aboutseparation of concerns

Please forget MVC, MVP, MVx...

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

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...

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

Pleaseremember!Model is not only about databasemanipulation!

This is where the business logic lives andprospers.

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

I'll show you one extremelyimportant design pattern

Dependencyinjection

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

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...

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."

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)

"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.

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>;

}

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...

$ 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?

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...

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';

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>

Now the controllersIt is always about recieving request and creating response

Nette\Application\IPresenterIt works just like this:

IPresenter#run( AppRequest ): IResponse

IRouter#match( HttpRequest ): AppRequest

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

UI\Presenter#renderShow(): void

UI\Presenter#actionShow(): void

UI\Presenter#handleSignal(): void

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

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

Doctrine 2MVPLatteNette DI

Symfony ConsoleUUIDTracyPHP 5.6+!

Here you go

Form + LoginNEONIdRouter

Do not! #1

@(Based on your GitHub repo)

This is called "shut the fuck up" operator

Do not! #2

if ($page == "")

$page = "uvod";

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

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

echo "<head>";

echo "</head>";

echo "<body>";

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

// ...

There is no reason for that.

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+

Do not! #5

global $variable

$GLOBALS['variable']

// + pseudoglobals

This is just OMG, FFUUU!

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?

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!

And yes, I am sorry...

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

Please visit zlml.czFollow me if you want @mrtnzlml