Procedural micro-framework for PHP server and JavaScript frontend.
The general purpose is to provide:
- Easy-to-use overridable global functions and constants.
- Appropriate functions available both at server and client side.
- Clear naming convention.
- JSON-like variable types.
- Guideline to organize modules in your project.
Fastred keeps programming as short and simple as possible and tries to exclude overhead from complex programming constructions, like classes and namespaces. All functions are global.
This is not always what you may need.
If you make something big and scalable with pretty large team you'd better use some other framework.
Composer cannot autoload global functions, so with Fastred you'll have to use fastredRequire()
to load needed modules (see Usage).
However, Fastred can be easily combined with any other framework or coding style if you want to use it partially.
It has some useful basic modules, like img
, pgn
or sql
and can be extended with additional libraries.
File of code that contains global functions and constants and follows naming convention.
Module that is implemented both in PHP and JavaScript in whole or in part.
You can use composer, so Fastred will be included with autoload.php
:
composer require dehero/fastred
Or you can extract Fastred distributive somewhere into your project's directory, then:
require 'path/to/fastred.php';
Fastred provides two basic functions:
Add folder path to search for modules in.
Require modules found in all added paths.
Provide one or more directories to search modules in. Basic Fastred modules are included with fastred.php
// Add folder paths to search for modules in
fastredLibrary(__DIR__ . '/modulesA');
fastredLibrary(__DIR__ . '/modulesB');
// Require modules module1.php, module2.php, module3.php
fastredRequire($module1, $module2, ...);
fastredRequire($module3);
// Use functions and constants from modules
module1Function($arg);
module2Function($arg);
echo MODULE3_CONST;
- Webpack
npm install fastred
fastredLibrary(require.context('./js'));
thisIsFunction($paramA, $paramB)
object->someProperty
$someVariable
THIS_IS_CONSTANT
thisIsFunction(paramA, paramB)
obj.someProperty
someVariable
THIS_IS_CONSTANT
- Function names should always be written in camelcase. Names of functions start with module ident, like
img*
,url*
orpageData*
. - Main function of the module is named by module ident itself, like
url()
. - Functions with names containing words
Get
,Of
, likescriptGetUrl()
orurlOfPage()
pass main result through their return. - Functions with names containing verb in present simple, like
emailIsValid()
orfileExists()
, usually return boolean result. - Functions with name containing
To
orFrom
are for conversion. - Other functions create, declare or change some existing things.
Object properties should always use camelcase code style, like $fastred->thisIsProperty
.
If property name is too long being written in single camelcase phrase, that's a
sign that properties should be nested, like $fastred->app->page->data
.
There are two reasons why objects but not arrays are used for stroring complex data in Fastred:
-
$data->key->subkey
is shorter and easier to write, than$data['key']['subkey']
. -
Objects are passed by references, so PHP doesn't need to waste time cloning object data for functions.
To check if an object has any properties it's best to use function
objHasProperties($object)
from obj
module.
Module | Universal | Purpose |
---|---|---|
app | Application lifecycle, routing, error handling | |
arr | Yes | Arrays |
cache | Yes | Caching values in memory just for current script |
css | ||
datetime | Yes | Working with date and time |
dir | Yes | Directories |
Sending email | ||
file | ||
img | Images | |
json | Yes | JSON |
obj | Yes | Objects (reference-passed associative arrays) |
page | ||
path | Yes | |
pgn | Pagination | |
script | ||
sql | Constructing SQL expressions | |
str | Yes | Strings |
url | Constructing and parsing URLs | |
var | Yes | Variables of all types |
Fastred can be useful with pug:
- Create
.pug
template using universal Fastred functions for presentation code. - Compile template to PHP with pug-php, having
expressionLanguage
set tojs
. - Compile template to JavaScript with pug (you can use pug-loader for webpack or other bundler).