PHP Classes

File: cor.php

Recommend this page to a friend!
  Classes of Malik Naik  >  PHP Design Patterns Repository  >  cor.php  >  Download  
File: cor.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Design Patterns Repository
Implementation of common design patterns in PHP
Author: By
Last change:
Date: 8 months ago
Size: 1,415 bytes
 

Contents

Class file image Download
<?php

require_once 'vendor/autoload.php';

use
DesignPattern\ChainOfResponsibility\Request;
use
DesignPattern\ChainOfResponsibility\Authenticate;
use
DesignPattern\ChainOfResponsibility\Authorize;
use
DesignPattern\ChainOfResponsibility\Welcome;

// Instantiate few requests.
$admin = new Request('admin@example.com', '1234');
$customer = new Request('customer@example.com', '1234');
$nonuser = new Request('testing@example.com', '8080');

// Instantiate the Authenticate and Authorize objects.
$authenticate = new Authenticate;
$authorize = new Authorize;
$welcome = new Welcome;

// Set the chain of responsibility
$authenticate->setNext($authorize);
$authorize->setNext($welcome);

// Handle some requests.
print "Admin\n";
print
"=========================\n";
$authenticate->handle($admin);
print
"\nCustomer\n";
print
"=========================\n";
$authenticate->handle($customer);
print
"\nNon user\n";
print
"=========================\n";
$authenticate->handle($nonuser);

// Outputs:
//
// Admin
// =========================
// Authenticate: User successfully authenticated.
// Authorize: User authorized as admin.
// Welcome back, Administrator.

// Customer
// =========================
// Authenticate: User successfully authenticated.
// Authorize: User authorized as customer.
// Welcome back, regular customer.

// Non user
// =========================
// Authenticate: Unable to authenticate user.

For more information send a message to info at phpclasses dot org.