I am using ZF2 and I want to use Zend_Config_Xml. looking at the library, the directory structure looks like so...
Zend/Config/Config.php
Then there is
Zend/Config/Reader/Xml.php
But that does not contain the class either. Where is the Zend_Config_Xml class ???
There is no Zend_Config_Xml class in ZF2, Zend_Config_Reader_Xml is the equivalent. (Zend_Config_Xml was a ZF1 thing.)
The docs has usage examples: http://framework.zend.com/manual/2.2/en/modules/zend.config.reader.html#zend-config-reader-xml
Related
I need to understand how to load classes in php. I want to access classes methods by instantiate the class only with its name, for example:
$foo = new Foo();
$foo->method();
And not like this:
$foo = new \Class\Bar\Foo();
I'm in a Symfony 2.7 environment.
Thanks in advance!
EDIT
My bad, I should have specified that I'd like to use classes like this in the whole project, not only in a few files.
You should use those classes (i.e. import them) like:
use strrife\MyBundle\Entity as Entity;
...
$e = new Entity();
The official docs can be found here.
PS
If you're using some IDE (for example, PHPStorm which personally I highly recommend), then, when you type Entity and this class wasn't imported yet, it gives you a list of options to import that class from.
EDIT
You shouldn't do that.
Importing the classes like that in the entire project is actually a bad idea because that might cause naming conflicts (and probably will if you'd like to do that for Symfony classes).
As a workaround for your project classes you can add a few lines to composer.json specifying the autoload paths to all your project folders.
You can also:
put all your entities to one single folder (very bad)
or write a custom __autoload() function that would iterate through files and folders searching for your class (docs), but it'd bring the same problems + some performance issues.
But I highly discourage you from doing that because... well, namespaces bring order and structure and you're likely to end up with class redeclarations.
I'm using silex for one of my projects and usually when i want render a twig template I use this syntax :
$app['twig']->render('page.twig');
in this project I want to use the trait to have this syntax :
$app->render('page.twig');
to do that I modified the Application class in Silex\Application and added :
use TwigTrait;
so my question is : if its ok to do that and modify this class or if not, is there another way to do that.
thanks in advance and I apologize for my bad English.
Instead of modifying the class, just extend it. The Silex test suite has an example. Then just use it like you would the normal application class.
$app = new MyTwigApp();
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.
In Codeigniter, when we use $this->load('class_name') in the controller, CI will try to create an instance of the class/model using its constructor.
But sometimes, I don't actually need an instance from that class, I just want to call some static functions from it. Also, there is a big limitation with $this->load('class_name'), it does not allow me to pass parameters to the constructor (unless we extend or modify the core class of CI).
I think the $this->load('class_name') function should only do a require_once on the class php file for me, and let me freely do things (create instance/call static functions) with the class in the controller.
Should I simply ignore this function and use require_once or writing my own __autoload function to load up the classes? This way, I just feel strange because it seems I am not writing codes inside the CI box.
You can pass parameters to your constructor. See the "Passing Parameters When Initializing Your Class" section in the user guide.
I found CodeIgniter's object creation and loading to be very limiting. I want full control over my code, and little magic in the background. I have instead started using Doctrine's Class Loader. It's very lightweight and is essentially SPL autoloading (also a good alternative). You don't need the whole Doctrine shebang with ORM and all that stuff, just the ClassLoader. There's some configuration tinkering to get this right, but it works wonders.
With PHP 5.3 I now have namespaced classes in the Application directory. For instance I created a new class in the Tests directory: Application\Tests\SomeTest.php
That test could look something like this:
namespace Tests;
class SomeTest {
...
}
I would use this class in my code (controllers, views, helpers) by simply using the fully qualified namespace (i.e. $test = new \Tests\SomeTest) or a "use" statement at the top of my code (use \Tests\SomeTest as SomeTest).
In this way I intend to replace all libraries and models with OO namespaced variants. There are many benefits to this: fast autoloading with SPL, full IDE intellisense support for classes/methods (CodeIgniter is really bad for that), your code is more portable to other frameworks or projects.
That said, I still use a lot of the CodeIgniter engine. This basically means I have $CI =& get_instance() in most of my classes. It's still a work in progress and I think the main reason I need CI is for it's database access. If I can factor that out ... and use something like Dependency Injection, then I won't need CodeIgniter in my classes at all. I will simply be using it for it's MVC framework, and using it's methods occasionally in my controllers.
I know this goes above and beyond your question, but hopefully it's some food for though - and it helps me to get it in writing too.
I'd like to play a bit with functional programming in PHP. I think it's intersting to try out something different when always writing oop in php.
However, I'd like to use namespaces. My namespaces are equal to the file structure:
namespace my\Models\User;
/my/Models/User.php
Is their a way to do something like autoloading for this. That I want have to write both use and require_once?
Best regards,
Sebastian
You cannot autoload functions or namespaces, only classes.