Browsed by
Snippet Category: php

composer autoload

composer autoload

<?php require "vendor/autoload.php"; use \jsmcm\Logger\Log; use \manfred\Reader\Read; $oLog = new Log(); $oRead = new Read();

spl_autoload_register

spl_autoload_register

<?php use \jsmcm\Logger\Log; use \manfred\Reader\Read; spl_autoload_register(function($class){ $baseDirectory = __DIR__.’/src/’; $classPath = str_replace("\\", "/", $class).".php"; if (file_exists($baseDirectory.$classPath)) { require $baseDirectory.$classPath; } }); $oLog = new Log(); $oRead = new Read();

Require Class

Require Class

<?php require __DIR__."/src/jsmcm/Logger/Log.php"; require __DIR__."/src/manfred/Reader/Read.php"; use \jsmcm\Logger\Log; use \manfred\Reader\Read; $oLog = new Log(); $oRead = new Read();

composer example

composer example

<?php include __DIR__."/vendor/autoload.php"; use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger(‘usdToZar’); $log->pushHandler(new StreamHandler(‘/var/www/html/test/log’, Logger::DEBUG)); // add records to the log $log->info("About to get exchange rate"); $client = new \GuzzleHttp\Client(); try { $res = $client->request(‘GET’, ‘http://api.softsmart.co.za/exchange/’); } catch (Exception $e) { print "An error occurred and has been logged. Please check the log file for info!"; $log->error("http request error resulted in an error", array("error"=>$e)); exit(); } $log->info("http result: ".$res->getStatusCode()); if ($res->getStatusCode() != 200) { $log->error("http request…

Read More Read More

composer bin

composer bin

sudo mv composer.phar /usr/local/bin/composer sudo chmod +x /usr/local/bin/composer echo "PATH=/usr/local/bin:$PATH" >> ~/.bash_profile

trait_example_index

trait_example_index

<?php include_once(dirname(__FILE__)."/class.Toy.php"); include_once(dirname(__FILE__)."/class.Car.php"); $oToy = new Toy(); print "Toy name: ".$oToy->getName()."<p>"; print "Toy colour: ".$oToy->getColour()."<p>"; $oCar = new Car(); print "Car model: ".$oCar->getModel()."<p>"; print "Car colour: ".$oToy->getColour()."<p>"; ?>

trait.colour.php

trait.colour.php

<?php trait Colour { function getColour() { return "red"; } } ?>

class.Toy.php_new

class.Toy.php_new

<?php include_once(dirname(__FILE__)."/trait.Colour.php"); class Toy { use Colour; function getName() { return "doll house"; } } ?>

class.Car.php_new

class.Car.php_new

<?php include_once(dirname(__FILE__)."/trait.Colour.php"); class Car { use Colour; function getModel() { return "jeep"; } } ?>