PHP OOP website example

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
AlfaLyr
Posts: 18
Joined: Fri Apr 15, 2016 11:08 am UTC
Contact:

PHP OOP website example

Postby AlfaLyr » Tue Apr 19, 2016 7:23 am UTC

Hi, I have a very simple website and I use PHP. I want to rewrite it in a OOP approach, does anyone know an example of a simple site written this way? I'm familiar with OOP coding, and I know basic PHP, but I don't know the "proper way" to use it in a website. Thank you!
1060 nm : a scientist girl life!

User avatar
raudorn
Posts: 343
Joined: Mon Jun 13, 2011 11:59 am UTC

Re: PHP OOP website example

Postby raudorn » Tue Apr 19, 2016 11:49 am UTC

All visible-source examples I know are using full-blown frameworks like Zend or Slim, which might or might not be overkill to go look for inspiration.

I guess most of the concepts boil down to this:
  1. A request comes in, either being called directly by the user or a redirect by the webserver, and lands in a file traditionally called index.php. This file is ideally the only procedural style code in the whole website.
  2. This file then bootstraps the necessary classes telling them that a request came in and could they please get up and handle it.
  3. The classes do their thing, which usually includes cookies (for sessions), routing (since the webserver redirected everything that's not an asset back to the index.php), form processing and controlling logic for the MVC pattern.
  4. Then a template is rendered which represents what's getting sent back to the client. Smart frameworks prerendered most of the response and just parameterize stuff like the username and so on. Though there is the depressing trend of making everything from scratch for every request.
  5. The classes did their stuff and whatever was output by the template is handled by PHP automatically as the body of the HTTP response.

Let's look at an example, spoilered for brevity:

Spoiler:
Procedural style index.php

Code: Select all

<?php
if (formIsValid($_POST)) {
   echo "Valid form.";
} else {
   echo "Invalid form";
}


OOP style index.php

Code: Select all

<?php
$form = new Form($_POST);

if ($form->isValid()) {
   $templateName = "forms/valid.phtml";
} else {
   $templateName = "forms/invalid.phtml";
}

$template = new Template($templateName);
$template->render();

User avatar
Xanthir
My HERO!!!
Posts: 5198
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: PHP OOP website example

Postby Xanthir » Tue Apr 19, 2016 11:20 pm UTC

As another example, here's the core page for my blog (with some irrelevant debugging cruft omitted):

Code: Select all

<?php
include_once 
"include.php";

if(
 isset($_GET['key'] ) ) {
    include_once "class.singlepost.php";
    $page = new SinglePost($_GET['key']);
    echo $page->toHTML();
    die();
}
 else {
    include_once "class.frontpage.php";
    $page = new FrontPage();
    echo $page->toHTML();
    die();
}
?>


The SinglePost and FrontPage classes know how to load the data they need and render themselves. They also delegate some of their rendering to CommentForm and Comment classes.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
AlfaLyr
Posts: 18
Joined: Fri Apr 15, 2016 11:08 am UTC
Contact:

Re: PHP OOP website example

Postby AlfaLyr » Wed Apr 20, 2016 5:33 am UTC

Thank you very much for your answers, that was very useful
1060 nm : a scientist girl life!

AffinityDesigner
Posts: 10
Joined: Thu Apr 28, 2016 3:40 pm UTC

Re: PHP OOP website example

Postby AffinityDesigner » Tue May 10, 2016 3:23 pm UTC

Not that this matters since you seems to have already gotten the answers that you need, but why OOP over PHP?

User avatar
Xanthir
My HERO!!!
Posts: 5198
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: PHP OOP website example

Postby Xanthir » Tue May 10, 2016 5:39 pm UTC

Those aren't competing things. "object oriented programming" is something you can do in PHP. I believe the OP was referring to doing oop as opposed to the default "just throw shit together and get code on the page" that PHP makes so easy.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests