php newbie class - php

class Theme
{
function __construct()
{
}
function load( $folder, $file )
{
$theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php';
require_once($theme_path);
return TRUE;
}
}
on index.php
<?php
require class.theme.php
$theme = new Theme;
$theme->load('site','index');
?>
on my site/index.php
<?php
// to work i need another $theme = new theme; why can i do this ? can i make
it make it work twice or more inside the function load ?
$theme->load('site','header');
$theme->load('site','footer');
?>
somehow it needs to $theme = new Theme; again on site/index.php
is there another way to make it work? maybe my class design is not good or algorithm is failing.
edit* more information
ok so what im trying to do is to load header view footer view.

We don't know the relationship between your two .php files so it would be difficult to answer.
If you define $theme as new theme, scoping rules still apply: you definition/instanciation is only valid on its scope. You won't have a global theme object. Independtly from any class/object design.

The object "$theme" doesn't persist throughout several files, so when "site/index.php" is requested, your object from "index.php" is gone ...
Either that or I got your question completely wrong :)

Try to make load function public:
class Theme
{
function __construct()
{
}
public static function load( $folder, $file )
{
$theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php';
require_once($theme_path);
return TRUE;
}
}

class Theme
{
function __construct()
{
}
function load( $folder, $file )
{
$theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php';
return $theme_path;
}
}
on index.php
<?php
require class.theme.php
$theme = new Theme;
require_once $theme->load('site','index');
?>
on my site/index.php
<?php
// to work i need another $theme = new theme; why can i do this ? can i make
it make it work twice or more inside the function load ?
require_once $theme->load('site','header');
require_once $theme->load('site','footer');
?>
this done the trick for the while, thanks guys.

Related

edit the output of jdoc:include type=head via renderer/head.php alter

I would like to order nicely the head section of a joomla site. After search of the forums I have come across this one http://forum.joomla.org/viewtopic.php?f=642&t=671526&p=3283757#p3283757
There a nice suggestion is to copy the /renderer/head.php file into the template folder and alter it to current needs.
They suggest
Blockquote
The render function in head.php not uses the $name var, so it`s fine to use to separate the js and metatags with css files and use the jdoc statement like this:
jdoc:include type="head" name="head" <-- will include all exept js (into
the head section)
jdoc:include type="head" name="foot" <-- for the js (before body tag closes)
Blockquote
But I simply have no idea how to implement this.
HAve someone experience with editing head.php in Joomla? I would appreciate any help.
I investigated a little bit about it and it seems a little bit hacky to do it.
This solution is currently working on Joomla 3.*.
First of all you have to modify /librabies/joomla/document/document.php.
Once you are in there update the function loadRenderer() from this:
public function loadRenderer($type)
{
$class = 'JDocumentRenderer' . $type;
if (!class_exists($class))
{
$path = __DIR__ . '/' . $this->_type . '/renderer/' . $type . '.php';
if (file_exists($path))
{
require_once $path;
}
else
{
throw new RuntimeException('Unable to load renderer class', 500);
}
}
if (!class_exists($class))
{
return null;
}
$instance = new $class($this);
return $instance;
}
To this :
public function loadRenderer($type)
{
$class = 'JDocumentRenderer' . $type;
if (!class_exists($class))
{
$path = __DIR__ . '/' . $this->_type . '/renderer/' . $type . '.php';
$app = JFactory::getApplication('site');
$path_custom = JPATH_THEMES . '/' . $app->getTemplate() .'/html/renderer/' . $type . '.php';
if (file_exists($path_custom))
{
require_once $path_custom;
}
elseif (file_exists($path))
{
require_once $path;
}
else
{
throw new RuntimeException('Unable to load renderer class', 500);
}
}
if (!class_exists($class))
{
return null;
}
$instance = new $class($this);
return $instance;
}
Actually the new code is looking for a render file in your template directory.
Now you are allow to copy libraries/joomla/document/html/renderer/head.php to templates/TEMPLATE_NAME/html/renderer/head.php and modify it.
If you want to use those :
<jdoc:include type="head" name="head" />
<jdoc:include type="head" name="foot" />
Update templates/TEMPLATE_NAME/html/renderer/head.php to this version here.
Another option (for joomla 2.5/3.0 and with slightly adjustments joomla 3.5.x) as mention here is the following:
1.) grap the "/libraries/joomla/document/html/renderer/head.php" from a Joomla 3.0 installation ZIP file
2.) rename that to "head_renderer.php" and put that into your template folder
3.) in your template index.php add:
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'head_renderer.php';
4.) If you are still using Joomla 3.0 you are fine, if you are using Joomla 3.5 edit head_renderer.php and change JDocumentRendererHead to JDocumentRendererHtmlHead.
5.) Adjust the head_renderer.php so that it fits your requirements

Wordpress: register_activation_hook not working

I currently writing a wordpress plugin, and i encountered some problem.
my function does not run upon plugin activation... can somebody tell me where is the proble?
class my_plugin {
public $ajaxurl;
public $excanvas;
public $plugin_path = '';
function __construct()
{
register_activation_hook(__FILE__,array(&$this, 'install'));
}
public function wpmon_install()
{
//Copy my page template to current theme.
$plugin_path = getcwd() . DIRECTORY_SEPARATOR . 'wp-content'. DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'wpmon' . DIRECTORY_SEPARATOR;
$target_path = get_stylesheet_directory(). DIRECTORY_SEPARATOR;
$target_path = str_replace('/',DIRECTORY_SEPARATOR,$target_path);
$template_files = glob($plugin_path . 'page_template'.DIRECTORY_SEPARATOR.'*.php');
foreach($template_files as $files)
{
$basename = basename($files);
try{
$target = $target_path . $basename;
copy($files , $target);
}
catch(Exception $e){
$this->log_error( $e->getMessage());
}
}
}
but unfortunately the install function not working...
but when outside class this code inside 'install' func is working
From the posted code I can see two problems. install should be changed to wpmon_install. Also you should add some code to initialize your class.
i.e. $wp_mon = new my_plugin(); after class my_plugin { }
Please check if you're using softlink, the plugin must exactly in /path/to/wordpress/wp-content/plugins/yourplugin, meaning to say that yourplugin is not a softlink.
You have Wordpress trying to call a function called install in your class, but it doesn't exist. So try changing:
register_activation_hook(__FILE__,array(&$this, 'install'));
to
register_activation_hook(__FILE__,array(&$this, 'wpmon_install'));

what is the usage of `spl_autoload_extension() `with `spl_autoload_register()`?

I am using spl_autoload_register() function to include all files . What i want that any class having extension .class.php or .php would includes directly. I made below class and register two different function and everything working fine, BUT
I think there is some way so that i need to register only one function to include both extensions together.
please have a look on my function and tell me what i am missing
my folder structure
project
-classes
-alpha.class.php
-beta.class.php
-otherclass.php
-includes
- autoload.php
-config.inc.php // define CLASS_DIR and include 'autoload.php'
autoload.php
var_dump(__DIR__); // 'D:\xampp\htdocs\myproject\includes'
var_dump(CLASS_DIR); // 'D:/xampp/htdocs/myproject/classes/'
spl_autoload_register(null, false);
spl_autoload_extensions(".php, .class.php"); // no use for now
/*** class Loader ***/
class AL
{
public static function autoload($class)
{
$filename = strtolower($class) . '.php';
$filepath = CLASS_DIR.$filename;
if(is_readable($filepath)){
include_once $filepath;
}
// else {
// trigger_error("The class file was not found!", E_USER_ERROR);
// }
}
public static function classLoader($class)
{
$filename = strtolower($class) . '.class.php';
$filepath = CLASS_DIR . $filename;
if(is_readable($filepath)){
include_once $filepath;
}
}
}
spl_autoload_register('AL::autoload');
spl_autoload_register('AL::classLoader');
Note : there is no effect on line spl_autoload_extensions(); . why?
i also read this blog but did not understand how to implement.
There is nothing wrong with the way you do it. Two distinctive autoloaders for two kinds of class files are fine, but I would give them slightly more descriptive names ;)
Note : there is no effect on line spl_autoload_extensions(); . why?
This only affects the builtin-autoloading spl_autoload().
Maybe it's easier to use a single loader after all
public static function autoload($class)
{
if (is_readable(CLASS_DIR.strtolower($class) . '.php')) {
include_once CLASS_DIR.strtolower($class) . '.php';
} else if (is_readable(CLASS_DIR.strtolower($class) . '.class.php')) {
include_once CLASS_DIR.strtolower($class) . '.class.php';
}
}
You may also omit the whole class
spl_autoload_register(function($class) {
if (is_readable(CLASS_DIR.strtolower($class) . '.php')) {
include_once CLASS_DIR.strtolower($class) . '.php';
} else if (is_readable(CLASS_DIR.strtolower($class) . '.class.php')) {
include_once CLASS_DIR.strtolower($class) . '.class.php';
}
});
Maybe this will help:
http://php.net/manual/de/function.spl-autoload-extensions.php
Jeremy Cook 03-Sep-2010 06:46
A quick note for anyone using this function to add their own autoload
extensions. I found that if I included a space in between the
different extensions (i.e. '.php, .class.php') the function would not
work. To get it to work I had to remove the spaces between the
extensions (ie. '.php,.class.php'). This was tested in PHP 5.3.3 on
Windows and I'm using spl_autoload_register() without adding any
custom autoload functions.
Hope that helps somebody.

Automatically choose a file location before loading it in php

I need to create a simple file overloading system like symfony does with php files and templates. I will give an example to explain what I need:
Given this folder structure:
- root_folder
- modules
-module1
-file1.php
-file2.php
-file3.php
- specific_modules
-module1
-file2.php
I would like to find a way that automatically loads a file if it is found inside the specific_modules folder (file2.php) when called, if it is not found, it should load file2.php normally from the modules directory.
I would like to do it unobstrusively for the programmer, but not sure if it's possible!!
Any help or advice is welcome, thanks in advance!
skarvin
If the files contain only objects with the same name, then you can write your own autoloader function and register it with spl_autoload_register(). Perhaps something like
function my_loader($class)
{
// look in specific_modules dir for $class
// if not there, look in modules dir for $class
}
spl_autoload_register('my_loader');
This will allow you to code simply as:
$obj = new Thing();
And if Thing is defined in specific_modules, it will use that one, else the default one.
$normal_dir = 'modules';
$specific_dir = 'specific_modules';
$modules = array('module1' => array('file1.php','file2.php','file3.php'));
foreach($modules as $module => $files)
{
foreach($files as $file)
{
if(!file_exists("$specific_dir/$module/$file"))
{
include("$normal_dir/$module/$file");
}
else
{
include("$specific_dir/$module/$file");
}
}
}
This code will work as simply for you as possible, it makes it easy to add new files to your modules and change the directory names. By "load" I am making the assumption you mean include, but that part is easy enough to change.
Similarly to Alex's answer, you could also define an __autoload function:
function __autoload($class_name) {
if (file_exists(__DIR__ . '/specific_modules/' . $class_name . '.php')) {
require __DIR__ . '/specific_modules/' . $class_name . '.php';
}
elseif (file_exists(__DIR__ . '/modules/' . $class_name . '.php')) {
require __DIR__ . '/modules/' . $class_name . '.php';
}
else {
// Error
}
}
Then if you do $obj = new Thing(); it will try to load Thing.php from those two directories.

CodeIgniter style $this->modelname->function() calls

What I'm trying to do is access models in the CodeIgniter style, by calling $this->model_name->function() in my controllers. heres what I've done so far:
$this->load = new Load();
foreach (glob("application/models/*.php") as $file) {
$model = basename($file, ".php");
$this->$model = new $model;
}
I know I can't do what I tried there, but hopefully you can see my objective. What I want to do is make PHP write $this->modelname = new modelname; for each file in the model folder. I'm grabbing all the PHP files and stripping the directory and .php to leave just the file name.
I apologize if this is hard to understand, its hard to explain >.<
It actually should work, you just need to actually include the file:
foreach (glob("application/models/*.php") as $file) {
#include_once $file;
$model = basename($file, ".php");
if (class_exists($model)) {
$this->$model = new $model;
} else {
//handle error, trow an exception?
}
}
Why not create an autoloader?
spl_autoload_register('customAutoloader');
function customAutoloader($class_name){
$file_name = $class_name . ".php";
//Model
if(substr($class_name, -5) === "Model"){
if(is_readable(PATH_MODELS . $file_name)){
require_once(PATH_MODELS . $file_name);
}
}
}
In this situation, all model classes must be called ModelnameModel, and files should be named the same - ModelnameModel.php and be located in PATH_MODELS.

Categories