syntax error, unexpected T_STATIC - php

I have this error while trying to use late static bindings. All I can find in google about this error is that people didn't have PHP5.3, but I have version 5.3.6.
Could someone help me please ?
Thanks
class Media
{
private $nom,
$ext;
public function ext_autorisees() { return array(); }
public function __construct( $fichier, $thumb = false )
{
$fichier = explode( ".", $fichier );
$nom = $fichier[0];
$ext = $fichier[1];
if( in_array( strtoupper( $ext ), static::ext_autorisees() ) )
{
if( strpos( $nom, "thumb_" ) === 0 && !$thumb )
throw new Exception("");
}
else
throw new Exception("");
$this->nom = $nom;
$this->ext = $ext;
}
public function getNom() { return $this->nom; }
public function getExt() { return $this->ext; }
public function getPath() { return $this->getNom() . "." . $this->getExt(); }
public function getThumb() { return "thumb_" . $this->getPath(); }
}

there is a problem in static::ext_autorisees()

Related

How to Get List All Classes and Methods from HMVC Codeigniter?

I have some modules in my HMVC, is it possible to get lists all classes and methods in the modules directory and put to an array?
Thank You and sorry for my bad English
Try this .. make a library with a name of "Controllerslist" and copy this code ..
class Controllerslist {
private $CI;
private $aControllers;
function __construct() {
$this->CI = get_instance();
$this->setControllers();
}
public function getControllers() {
return $this->aControllers;
}
public function setControllerMethods($p_sControllerName, $p_aControllerMethods) {
$this->aControllers[$p_sControllerName] = $p_aControllerMethods;
}
private function setControllers() {
foreach(glob(APPPATH . 'modules/controllers/*') as $controller) {
if(is_dir($controller)) {
$dirname = basename($controller);
foreach(glob(APPPATH . 'modules/controllers/'.$dirname.'/*') as $subdircontroller) {
$subdircontrollername = basename($subdircontroller, EXT);
if(!class_exists($subdircontrollername)) {
$this->CI->load->file($subdircontroller);
}
$aMethods = get_class_methods($subdircontrollername);
$aUserMethods = array();
foreach($aMethods as $method) {
if($method != '__construct' && $method != 'get_instance' && $method != $subdircontrollername) {
$aUserMethods[] = $method;
}
}
$this->setControllerMethods($subdircontrollername, $aUserMethods);
}
}
else if(pathinfo($controller, PATHINFO_EXTENSION) == "php"){
$controllername = basename($controller, EXT);
if(!class_exists($controllername)) {
$this->CI->load->file($controller);
}
$aMethods = get_class_methods($controllername);
$aUserMethods = array();
if(is_array($aMethods)){
foreach($aMethods as $method) {
if($method != '__construct' && $method != 'get_instance' && $method != $controllername) {
$aUserMethods[] = $method;
}
}
}
$this->setControllerMethods($controllername, $aUserMethods);
}
}
}
}
after this.. in your controller load this library
$this->load->library('Controllerslist');
$list = $this->controllerslist->getControllers();
print_r($list);
This works for me ..
small update Utkarsh Tiwari
For HMVC structure we should get all modules list first then all controllers of each modules ..
class ControllerList {
// Codeigniter reference
private $CI;
// Array that will hold the controller names and methods
private $aControllers;
// Construct
function __construct() {
// Get Codeigniter instance
$this->CI = get_instance();
// Get all controllers
$this->setControllers();
}
/**
* Return all controllers and their methods
* return array
*/
public function getControllers() {
return $this->aControllers;
}
/**
* Set the array holding the controller name and methods
*/
public function setControllerMethods($p_sControllerName, $p_aControllerMethods) {
$this->aControllers[$p_sControllerName] = $p_aControllerMethods;
}
/**
* Search and set controller and methods.
*/
private function setControllers() {
foreach(glob(APPPATH . 'modules/*') as $modules_all) {
if(is_dir($modules_all)) {
$dirname = basename($modules_all);
foreach(glob(APPPATH . 'modules/'.$dirname.'/controllers/*') as $subdircontroller) {
$subdircontrollername = basename($subdircontroller, EXT);
if(!class_exists($subdircontrollername)) {
$this->CI->load->file($subdircontroller);
}
$aMethods = get_class_methods($subdircontrollername);
$aUserMethods = array();
foreach($aMethods as $method) {
if($method != '__construct' && $method != 'get_instance' && $method != $subdircontrollername) {
$aUserMethods[] = $method;
}
}
$this->setControllerMethods($subdircontrollername, $aUserMethods);
}
}
else if(pathinfo($controller, PATHINFO_EXTENSION) == "php"){
$controllername = basename($controller, EXT);
if(!class_exists($controllername)) {
$this->CI->load->file($controller);
}
$aMethods = get_class_methods($controllername);
$aUserMethods = array();
if(is_array($aMethods)){
foreach($aMethods as $method) {
if($method != '__construct' && $method != 'get_instance' && $method != $controllername) {
$aUserMethods[] = $method;
}
}
}
$this->setControllerMethods($controllername, $aUserMethods);
}
}
}
}
after this you can simply get all list of methods and controllers..
$this->load->library('controllerlist');
$list = $this->controllerlist->getControllers();
print_r($list);
this works for me..

PHP - Check if audio file is not corrupted

This is method for creating song object
public function getSong() {
return new Song($this->rackDir, $this->getLoadedDisc()->getName(), $this->song);
}
There is Song class
class Song extends CD {
private $id;
public function __construct($rack, $name, $id)
{
$this->id = $id;
parent::__construct($rack, $name);
}
public function getSelectedSongId() {
return $this->id;
}
public function getSelectedSongPath() {
$list = $this->getSongsList();
return $list[$this->id];
}
}
public function getSongInfo () {
$data = [
'name' => $this->getName(),
'size' => $this->getSize(),
];
return $data;
}
public function getSize() {
$path = $this->getPath() . '/' . $this->getName();
return filesize($path);
}
public function getName() {
return $this->getSelectedSongPath();
}
}
And there is CD Class where I check if file has audio extension.
class CD {
private $path;
private $name;
private $rack;
private $validExtensions;
public function __construct($rack, $name)
{
$this->rack = $rack . '/';
$this->name = $name;
$this->path = $this->rack . $this->name;
$this->validExtensions = ['mp3', 'mp4', 'wav'];
}
public function getPath() {
return $this->path;
}
public function getName() {
return $this->name;
}
public function getSongsList () {
$path = $this->rack . $this->name;
$songsList = [];
if (!is_dir($path)) {
return false;
}
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && in_array(strtolower(substr($file, strrpos($file, '.') + 1)), $this->validExtensions))
{
array_push($songsList, $file);
}
}
closedir($handle);
}
return $songsList;
}
}
I want to check if File is real audio file and not just file with an audio extension?
Is there is method to do that in PHP?
Karlos was in right.
I was found a solution with this code bellow.
public function validateFile (Song $song) {
$allowed = array(
'audio/mp4', 'audio/mp3', 'audio/mpeg3', 'audio/x-mpeg-3', 'audio/mpeg', 'audio/*'
);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$info = finfo_file($finfo, $song->getSelectedSongPath());
if (!in_array($info, $allowed)) {
die( 'file is empty / corrupted');
}
return $song;
}

unidentified php syntax error

I am trying to use a pre-written PHP autoloader and dreamweaver is stating that there is a syntax error. Can someone please explain what is causing the problem.
The lines which show as having syntax errors are
$autoloader = new static($dir, $ext);
spl_autoload_register([$autoloader, 'load']);
<?php
/**
* A basic PSR style autoloader
*/
class AutoLoader
{
protected $dir;
protected $ext;
public function __construct($dir, $ext = '.php')
{
$this->dir = rtrim($dir, DIRECTORY_SEPARATOR);
$this->ext = $ext;
}
public static function register($dir, $ext = '.php')
{
$autoloader = new static($dir, $ext);
spl_autoload_register([$autoloader, 'load']);
return $autoloader;
}
public function load($class)
{
$dir = $this->dir;
if ($ns = $this->get_namespace($class)) {
$dir .= DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $ns);
}
$inc_file = $dir.DIRECTORY_SEPARATOR.$this->get_class($class).$this->ext;
if (file_exists($inc_file)) {
require_once $inc_file;
}
}
// Borrowed from github.com/borisguery/Inflexible
protected static function get_class($value)
{
$className = trim($value, '\\');
if ($lastSeparator = strrpos($className, '\\')) {
$className = substr($className, 1 + $lastSeparator);
}
return $className;
}
// Borrowed from github.com/borisguery/Inflexible
public static function get_namespace($fqcn)
{
if ($lastSeparator = strrpos($fqcn, '\\')) {
return trim(substr($fqcn, 0, $lastSeparator + 1), '\\');
}
return '';
}
}
spl_autoload_register([$autoloader, 'load']);
This array syntax is only valid as of PHP 5.4. If DreamWeaver is using an older version of PHP syntax rules, it will complain.

zend framework 2 + custom routing

I tried to follow the recommendations from this topic: zend framework 2 + routing database
I have a route class:
namespace Application\Router;
use Zend\Mvc\Router\Http\RouteInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\Mvc\Router\RouteMatch;
class Content implements RouteInterface, ServiceLocatorAwareInterface {
protected $defaults = array();
protected $routerPluginManager = null;
public function __construct(array $defaults = array()) {
$this->defaults = $defaults;
}
public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $routerPluginManager) {
$this->routerPluginManager = $routerPluginManager;
}
public function getServiceLocator() {
return $this->routerPluginManager;
}
public static function factory($options = array()) {
if ($options instanceof \Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
}
if (!isset($options['defaults'])) {
$options['defaults'] = array();
}
return new static($options['defaults']);
}
public function match(Request $request, $pathOffset = null) {
if (!method_exists($request, 'getUri')) {
return null;
}
$uri = $request->getUri();
$fullPath = $uri->getPath();
$path = substr($fullPath, $pathOffset);
$alias = trim($path, '/');
$options = $this->defaults;
$options = array_merge($options, array(
'path' => $alias
));
return new RouteMatch($options);
}
public function assemble(array $params = array(), array $options = array()) {
if (array_key_exists('path', $params)) {
return '/' . $params['path'];
}
return '/';
}
public function getAssembledParams() {
return array();
}
}
Pay attention that the match() function returns object of the instance of Zend\Mvc\Router\RouteMatch
However in the file Zend\Mvc\Router\Http\TreeRouteStack it checks for object to be the instance of RouteMatch (without prefix of namespace)
if (
($match = $route->match($request, $baseUrlLength, $options)) instanceof RouteMatch
&& ($pathLength === null || $match->getLength() === $pathLength)
)
And the condition fails in my case because of the namespace.
Any suggestions?
Ok, i figured out what the problem was.
Instead of returning Zend\Mvc\Router\RouteMatch I should return Zend\Mvc\Router\Http\RouteMatch
This fixed my problem

Zend models and database relationships

i'm starting with Zend Framework and I'm a little bit confused with models and relathionships (one-to-many, many-to-many etc).
The "Zend Framework Quick Start" says to create a Zend_Db_Table, a Data Mapper and finally
our model class
Suppose we have a database like this:
table A (
id integer primary key,
name varchar(50)
);
table B (
id integer primary key,
a_id integer references A
);
then, i'll create:
Application_Model_DbTable_A extends Zend_Db_Table_Abstract,
Application_Model_AMapper,
Application_Model_A,
Application_Model_DbTable_B extends Zend_Db_Table_Abstract,
Application_Model_BMapper,
Application_Model_B,
if I understood, i've to store the references informations in
Application_Model_DbTable_A:
protected $_dependentTables = array('B');
and Application_Model_DbTable_B:
protected $_referenceMap = array(
'A' => array(
'columns' => array('a_id'),
'refTableClass' => 'A',
'refColums' => array('id')
)
);
and my models class:
class Application_Model_A
{
protected $_id;
protected $_name;
public function __construct(array $options = null)
{
if(is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
$this->$method($value);
}
public function __get($name)
{
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
return $this->$method();
}
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
public function setName($name)
{
$this->_name = (string) $name;
return $this;
}
public function getName()
{
return $this->_name;
}
public function setId($id)
{
$this->_id = (int) $id;
return $this;
}
public function getId()
{
return $this->_id;
}
class Application_Model_B
{
protected $_id;
protected $_a_id;
public function __construct(array $options = null)
{
if(is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
$this->$method($value);
}
public function __get($name)
{
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid property');
}
return $this->$method();
}
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
public function setA_id($a_id)
{
$this->_a_id = (int) $a_id;
return $this;
}
public function getA_id()
{
return $this->_a_id;
}
public function setId($id)
{
$this->_id = (int) $id;
return $this;
}
public function getId()
{
return $this->_id;
}
it's that right?
Well let's assume you have a db table called products. You want to access this table and work with the data.
First you need in models/dbtable/ folder a Product.php file to tell zf where to look for the data:
class Default_Model_DbTable_Product extends Zend_Db_Table_Abstract
{
protected $_name = 'products';
}
So $_name is the name of the db table. Second you need a class where you store all the information like product attributes. Let's say a product has just a name:
class Default_Model_Product{
protected $_name;
public function setName($name) {
$this->_name = $name;
return $this;
}
public function getName() {
return $this->_name;
}
public function __construct(array $options = null) {
if (is_array ( $options )) {
$this->setOptions ( $options );
}
}
//you need the methods below to access an instanz of this class
//put it in every model class
public function __set($name, $value) {
$method = 'set' . $name;
if (('mapper' == $name) || ! method_exists ( $this, $method )) {
throw new Exception ( 'Invalid product property' );
}
$this->$method ( $value );
}
public function __get($name) {
$method = 'get' . $name;
if (('mapper' == $name) || ! method_exists ( $this, $method )) {
throw new Exception ( 'Invalid product property' );
}
return $this->$method ();
}
public function setOptions(array $options) {
$methods = get_class_methods ( $this );
foreach ( $options as $key => $value ) {
$method = 'set' . ucfirst ( $key );
if (in_array ( $method, $methods )) {
$this->$method ( $value );
}
}
return $this;
}
}
Finally you need a mapper class where you actually access the data. You retrieve the data from the table and store it as objects in an array:
class Default_Model_ProductMapper {
protected $_dbTable;
public function setDbTable($dbTable) {
if (is_string ( $dbTable )) {
$dbTable = new $dbTable ();
}
if (! $dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception ( 'Invalid table data gateway provided' );
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable() {
if (null === $this->_dbTable) {
$this->setDbTable ( 'Default_Model_DbTable_Product' );
}
return $this->_dbTable;
}
//e.g. fetch all
public function fetchAll() {
$resultSet = $this->getDbTable ()->fetchAll ();
$entries = array ();
foreach ( $resultSet as $row ) {
$entry = new Default_Model_Product ();
$entry->setName ( $row->name );
$entries [] = $entry;
}
return $entries;
}
}
This is really straight forward ;)
I really recommend you to use another DB-Framework in your MVC-System.
Zend isn't quite nice with Databases and it's IMO too complicated/ too much code to write.
There are Frameworks which play very nice with Zend (VC) like PHPActiveRecord...
--> http://www.phpactiverecord.org/projects/main/wiki/Frameworks

Categories