I came across the __autoload function in PHP and would like to use it with my MVC folder structure. The function itsself is pretty easy, but how do I achieve a dynamic folder scanning after some kind of naming, please see example:
-application
--controller
--models
---entities
----house
---factories
----houseFactory
--views
-library
-public
As you maybe recognise its very close based on zend framework or other frameworks -as I come from those, however I would like to develop a website without framework and just started to write bootsrap file.
Maybe sombody could help me with the autoload in this - i think - advanced usage.
My Classnames will be like
Model_Entities_House
or
Model_Factory_HouseFactory
witch can be applied to the folder structure.
What I do mostly is use the SPL autoload function, which will help you to accomplish this quite easily. It should be something like this:
spl_autoload_register("MyClass::Autoloader");
Then, you can do something like this
class MyClass
{
public static function Autoloader($className)
{
//parse $className and decide where to load from...
}
}
If you´re using a naming convention, then you should be available to load the required file by just using the name.
Related
Say I want a constant, a function or a class to be available in all my models, views and controllers code (within a particular web site (application)). Where can I put them? I don't mind importing them explicitly in every file I need them in but I don't know how (simple require_once does not seem to work).
You can put them in the vendor folder (in application/vendor or modules/MOD/vendor). Then you can load it like this:
require Kohana::find_file('vendor', 'folder/file','ext');
You can read up more on this in the user guide
Now, it should be stated you should in general not use functions or globals.
I declare Constants in Bootstrap.php and create my own Helpers for general functions under application/classes/Helpers.
If you need to integrate a third party library into Kohana or want to make code available to other Kohana users consider creating a module instead.
You can define all your constants in new php file and place it in the application/classes directory. In your Template controller or in the main controller like Welcome or Website, just place the code in the __constructor()
require_once Kohana::find_file( 'classes', 'filename' );
I suggest you to put your constant variables in the bootstrap.php file because this is loaded by the framework before every request.
You can simply put your classes in the root of the classes directory, the framework will find.
This worked well for me...
In application/config/constants.php:
define('SOME_COOL_CONSTANT', 'foo');
return array();
In index.php:
Kohana::$config->load('constants');
SOME_COOL_CONSTANT should then be available globally.
I got a PHP library (PHP Markdown) in my library fold in an standard Zend Framework application. What is the best way to load the file and all it's classes to use in my models and controllers.
Structure:
library/phpMarkdown/markdown.php
Note:
PHP Markdown has a really ugly structure: It's only real "API" is a simple function, not a class. So the elegant was do not work for this exact case, but regarding the question the genearl solution the correctly named files/class is also "the right answer.
Edit
So much good input here, really not sure which answer I should accept! Thanks to you all!
The autoloader
Just instantiate the class and the autoloader should find it. If it doesn't you need to add the namespace and path.
If you have a class in the following tree (for exemple) : library/My/Tool.php
You will need to add this to your application.ini :
autoloaderNamespaces[] = "My_"
And then in your code you just call :
$tool = new My_Tool();
Edit :
in the file Tool.php you must follow the Zend Naming Conventions and have something like this :
<?php
class My_Tool {
}
For more informations see this : Zend Naming conventions
To keep it simple and just add that one file, you could put something like this in your Bootstrap.php:
protected function _initLoad(){
Zend_Loader::loadFile('markdown.php', '/../library');
}
I just copied markdown.php into the application library and put this little function in the bootstrap. You could also use Zend_Loader::loadClass(); if you want.
I'm building a website using CI, HMVC and Smarty. It's my first time using HMVC and I don't understand how I can use common libraries within the modules.
I prefer using Smarty on my sites and usually it's a piece of cake: I create a wrapper for smarty, autoload it and use it in controllers where necessary. But now I need to use smarty within the module controller and I don't know how to access it. Any ideas how I can do that?
I've been researching the issue for a couple of days, with no luck.
There are some answers which I just don't get: like this one
EDIT: CI 2.1.0, HMVC 5.4, Smarty 3.1.6 (but this is irrelevant)
Here are a couple of ways:
In application/libraries
Just put the common libraries in application/libraries folder and load using $this->load->library('my_library');
Create a common module
Another option would be to create a new module, lets say common, in which you can have folders libraries,models,helpers. Here you can put files which are common to other modules. You can then load them using $this->load->library('common/my_library');
I hope that helps.
You can extend your smarty in your modules with modules. path.
Example:
class MySmartie extends Smartie {
function __construct()
{
parent::__construct();
$this->template_dir = APPPATH . "modules/client/views/templates";
$this->compile_dir = APPPATH . "modules/client/views/templates_c";
}
}
And load this class in your modules class constructor like this:
public function __construct()
{
$this->load->library(['mysmartie' => 'smarty']);
}
Note: don't load smarty in config/autoload.php it may create conflict in loading.
I am learning how to use the Zend Framework. I come from a codeigniter background.
What I want to do is define a function somewhere that performs a very simple yet useful function. I am predominantly going to use the function within view scripts. I don;t really want to make a whole class for such a simple thing, so my question is, is there anywhere were can I put a file containg all of my general functions and how do I go about using it?
Thanks
John
What you are looking for are view helpers.
A view helper however is a function in a helper class. Therefore only one view helper can be put in a single class.
If you are using the project setup as used in the quick start tutorial or as generated by Zend_Tool, your view helpers should be put in the application/views/helpers directory.
Declaring a view helper is pretty simple, and is explained in great detail on this page of the zend framework documentation (i must say it's a bit hidden in the docs):
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.custom
Some background information on view helpers as well as some standard included ones can be found on this page: http://framework.zend.com/manual/en/zend.view.helpers.html
Hope this helped you in the right direction.
If you realy whant to use a function you can make a library class with a static method , make a folder like this Application/Library/MyLib , then at bootstrap register MyLib namespace like this
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLib'); , then inside MyLib folder you can make a filename MyClass , with a class name MyLib_MyClass , then inside you're view you can call MyLib_MyClass::staticMethod().
Tough i suggest you make a view helper for this . You don't realy use functions in ZF like you where used to in CI ( i was in you're exact situation a few months ago ) , ZF is all about OOP .
What I want to do is this:
When I write a class and the class instantiates another class, I want to import that class with an require_once. Just the way I do so in Objective-C. But instead of using plain require_once function and messing around with paths and string concatenation, I would prefer something like:
importClass('myclass');
but I'm afraid that it's impossible to write a function that will include code. If I would do that in the importClass() function, I would include the class code into the implementation block of the function, which of course is nonsense. So what options do I have here?
The cleanest way to do what you want looks to be to use the Autoloader
It's not impossible at all. You can write this function:
function importClass($class) {
require_once "$class.class.php";
}
The only caveat is that any global variables declared or used inside that file will now be local to importClass(). Class definitions however will be global.
I'm not sure what this really gives you however but you can certainly do it.
In my application I have a system base class which has a similar function. The import function takes a class name, looks in a couple of related directories and finds a matching name (I also did some stuff with extensions to libraries but you may not need that) and instantiates a class inside with the same name. Then it takes that new instance and sets it as an object in the system base class.
using autoload as other answers have suggested would probably work better in your situation but this is just another way to look at it.
You can accomplish something similar using a class autoloader. I would also make sure that your include_path is set properly and that you are using a directory structure that makes sense for your classes - it's generally a good practice to NOT depend on class autoloaders, and instead include classes based on their relative path to your include_path.
I'd highly recommend browsing through Zend Framework, particularly Zend_Loader, for a good (if not over-architected) implementation. Also notice that Zend Framework will work without an autoloader in place - each file calls require_once on its direct dependencies, using their nice, organized directory structure.