Browsed by
Snippet Category: php

class_use_trait

class_use_trait

class MyClass { use MyTrait; // methods.. }

trait_shell

trait_shell

trait MyTrait { // methods here.. }

index.php

index.php

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

class.Toy.php

class.Toy.php

<?php class Toy { function getName() { return "doll house"; } }

class.car.php

class.car.php

<?php class Car { function getModel() { return "jeep"; } } ?>

PHP Dependency Injection

PHP Dependency Injection

<?php class Log() { $protected writer = false; function __construct($writer) { $this->write = $writer; } public function log($message) { if( ($message == "") || ($this->write == false) ) { return; } $message = date("Y-m-d H:i:s")." – ".$message."rn"; $this->writer->log($message); } } class fileWriter { public function log($message) { file_put_contents("./fileWriter.log", $message, FILE_APPEND); } } class databaseWriter { public function log($message) { $db->insert("log_table", array("message"), array($message) ); } } // do some stuff $oFileWriter = new fileWriter(); $oDatabaseWriter = new databaseWriter(); // log to…

Read More Read More

Multiple Function Calls

Multiple Function Calls

<?php class Log() { public function logFile($message) { … some code here… } public function logDatabase($message) { … some code here… } } ?>

Log Class

Log Class

<?php class Log() { public function log($message) { if( $message == "" ) { return; } $message = date("Y-m-d H:i:s")." – ".$message."rn"; file_put_contents("./messages.log", $message, FILE_APPEND); } } // do some stuff $oLog = new Log(); $oLog->Log("all done!"); ?>

PHP opcache

PHP opcache

; enable the opcache opcache.enable=1; ; set validate_timestamps to 1 to cause PHP to check if the file ; has been modified. ; This check will happen every revalidate_freq seconds. ; If a file is older than revalidate_freq seconds AND it has changed ; it will be removed from the cache and recompiled next time the ; page is accessed. opcache.validate_timestamps=1 opcache.revalidate_freq=600 ; set this to 0 to cause PHP to check the timestamps on each access. ; optionally, you…

Read More Read More

Resque

Resque

apt-get install rubygems gem install resque