Trying to autoload classes from <root>/incl/classes folder.
The problem is, when I call some class for ex. ip like that
$ip= new ip();
$ip=$ip->get();
PHP gives error message Notice: Undefined variable: path . But in fact file already exists
I'm declaring all various paths at the top of page.
define("ds", DIRECTORY_SEPARATOR);
$path = array();
$path['root'] = $_SERVER['DOCUMENT_ROOT'];
$path['common'] = $path['root'] . ds . "common";
$path['design'] = $path['root'] . ds . "design";
$path['contents'] = $path['root'] . ds . "contents";
$path['content_images'] = $path['root'] . ds . "content" . ds . "images";
$path['design_images'] = $path['root'] . ds . "design" . ds . "images";
$path['blocks'] = $path['contents'] . ds . "blocks";
$path['includes'] = $path['root'] . ds . "incl";
$path['pages'] = $path['contents'] . ds . "pages";
$path['classes'] = $path['includes'] . ds . "classes";
$files = glob("common" . ds . "*.php");
array_unshift($files, $path['common'] . ds . "settings.php", $path['common'] . ds . "db.php");
foreach ($files as $filename)
require_once $filename;
//Auto loading classes
function __autoload($class_name) {
if (file_exists($path['classes'] . ds . $class_name . '.php')) {
require_once($path['classes']. ds . $class_name . '.php');
} else {
die($path['classes'] . ds . $class_name . '.php');
}
}
For testing purposes added die($path['classes'] . ds . $class_name . '.php'); line. It outputs \ip.php. I wonder, why it doesn't echo $path['classes'] even if I declared it before ?
It's a scoping issue. Your $path variable doesn't exist in your autoload function. It is a global variable, and you need to explicitly "invite" it:
function __autoload($class_name) {
global $path;
You should actually have gotten a notice about this.
Related
I'm trying to get this path:
C:\xampp\htdocs\site\folder\filename.jpg
By doing:
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\folder\
But it doesn't have the $filename.
I have also tried:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . 'folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\files\folder\filename.jpg
So I tried adding ..\ to get away from the files folder:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . '..\folder' . DIRECTORY_SEPARATOR . $filename);
But that returns:
C:\xampp\htdocs\site\files\..\folder\filename.jpg
How can I get to the correct folder?
$filename = 'filename.jpg';
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This should give the correct value
I am creating a custom MVC framework in PHP based on the tutorial by Anant Garg:
CustomMVC
-app
--controllers
---Controller.php
---HomeController.php
--models
---Home.php
--views
-config
--app.php
-lib
--bootstrap.php
--shared.php
--database.class.php
-public
--.htaccess
--index.php
-tmp
-.htaccess
My HomeController cannot extend the Controller and I get the following:
Fatal error: Class 'app\controllers\Controller' not found in
/Applications/MAMP/htdocs/CustomMVC/app/controllers/homecontroller.php
on line 7
Both of them are under the same namespace and they are located under the same directory. I don't know why HomeController cannot extend the base Controller. Any advice on that?
namespace app\controllers;
Controller.php
namespace app\controllers;
use lib\Template;
class Controller {
protected $_model;
protected $_controller;
protected $_action;
protected $_template;
public function __construct($model, $controller, $action) {
$this->_controller = $controller;
$this->_action = $action;
$this->_model = $model;
$this->_model =& new $model;
$this->_template =& new Template($controller,$action);
}
function set($name,$value) {
$this->_template->set($name,$value);
}
function __destruct() {
$this->_template->render();
}
}
HomeController.php
<?php
namespace app\controllers;
use app\controllers;
class HomeController extends Controller {
function getHome($id = null,$name = null) {
}
function getAbout() {
}
function postContact() {
}
}
This is the autoload function in the lib**shared.php**
function __autoload($className) {
if ( file_exists(ROOT . DS . 'lib' . DS . strtolower($className) . '.php') )
{
require_once(ROOT . DS . 'lib' . DS . strtolower($className) . '.php');
}
else if ( file_exists(ROOT . DS . 'lib' . DS . strtolower($className) . '.class.php'))
{
require_once(ROOT . DS . 'lib' . DS . strtolower($className) . '.class.php');
}
else if ( file_exists(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.php'))
{
require_once(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.php');
}
else if( file_exists(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.class.php'))
{
require_once(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.class.php');
}
else if (file_exists(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.php'))
{
require_once(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.php');
}
else if (file_exists(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.class.php'))
{
require_once(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.class.php');
}
else
{
/* Error Generation Code Here */
}
}
Following CakePHP code is in my index.php file. When it runs on the server, it shows
internal server error 500
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOT')) {
define('ROOT', dirname(dirname(dirname(__FILE__))));
}
if (!defined('APP_DIR')) {
define('APP_DIR', basename(dirname(dirname(__FILE__))));
}
$vendorPath = ROOT . DS . APP_DIR . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib';
$dispatcher = 'Cake' . DS . 'Console' . DS . 'ShellDispatcher.php';
if (!defined('CAKE_CORE_INCLUDE_PATH') && file_exists($vendorPath . DS . $dispatcher)) {
define('CAKE_CORE_INCLUDE_PATH', $vendorPath);
}
if (!defined('WEBROOT_DIR')) {
define('WEBROOT_DIR', basename(dirname(__FILE__)));
}
if (!defined('WWW_ROOT')) {
define('WWW_ROOT', dirname(__FILE__) . DS);
}
if (php_sapi_name() === 'cli-server') {
if ($_SERVER['REQUEST_URI'] !== '/' && file_exists(WWW_ROOT . $_SERVER['PHP_SELF'])) {
return false;
}
$_SERVER['PHP_SELF'] = '/' . basename(__FILE__);
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
}
if (!include 'Cake' . DS . 'bootstrap.php') {
$failed = true;
}
} else {
if (!include CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'bootstrap.php') {
$failed = true;
}
}
if (!empty($failed)) {
trigger_error("CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
}
App::uses('Dispatcher', 'Routing');
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(
new CakeRequest(),
new CakeResponse()
);
Go to your app/Config/core.php and change
Configure::write('debug',0);
to
Configure::write('debug',2);
Visit the same page you will get the detail about the error
My image directory is \webroot\files\thumbs
I am trying to add file_exists condition. For that I tried bellow code
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'jpg';
$file_exists = file_exists($file);
It's always returning zero.
If I echo $file it's giving me output like this
c:\xampp\htdocs\myproject\files\thumbs\_61.jpg
You're missing the . before the extension. Update your $file definition as follows:
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'.jpg';
// This was missing ---^
$file_exists = file_exists($file);
There's a magic function to autoload classes (__autoload), I want to know if there a way to load a file without a class.
Something like this:
require ('example_file'); // Trigger __autoloadfiles function
function __autoloadfiles($filename)
{
$files = array(
ROOT . DS . 'library' . DS . $filename. '.php',
ROOT . DS . 'application' . DS . $filename . '.php',
ROOT . DS . 'application/otherfolder' . DS . $filename. '.php'
);
$file_exists = FALSE;
foreach($files as $file) {
if( file_exists( $file ) ) {
require_once $file;
$file_exists = TRUE;
break;
}
}
if(!$file_exists)
die("File not found.");
}
You can define own function for requiring:
function require_file($file) {
// your code
}
and then call it
require_file('file');
I guess that there is no way to overload require function.