Use existing classes in prestashop module - class

I am preparing a custom module for prestashop. I want to use in it some classes that exist in prestashop already (Orderdetail.php).
How can I do this? Is the code below sufficient or do I need to include something else in addition?
$order = new OrderDetail;

Yes, the native classes can be called like that.
$order_detail = new OrderDetail();
However to use custom classes you need to include their files in the script you want to use them.
include_once 'path_to_custom_class_file';
class MyModule extends Module {
public function aMethod() {
$myClass = new MyClass();
}
}

To make an instance of your module you have to use this code:
$mymodule = Module::getInstanceByName('mycustommodule');
Than you can use method of your module core.
E.g.
$mymodule->myCustomMethod('x', 'y');

Related

Using MEF inside a class library

OK, so inside a class library, is it a good idea NOT to use MEF?
Here is an example:
ISomeInterface
5 Implementations of ISomeInterface
Once class that imports all ISomeInterface and uses them.
Again, this is all inside a single dll. Since it is in a DLL, there is no bootstrapping of MEF to create the catalog, and it seems a bit much to build a catalog just to use it once.
Am just learning MEF and how to use it.
Greg
After reading up a bit more on this, it looks like there is no reason that MEF can't be used inside a DLL for it's own parts creation. When I asked this question, I was thinking that the Importing would be mainly inside a Main() or App() type of function to compose the entire app. But if composing needs to be done on a major part that gets exported to the app, it can still use MEF to compose itself in the constructor, like this:
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
}

Where to put a class that is not a controller, module or view?

I have a ZF2 application made of several modules, and I want to create a Logger class that should be accessible from any of these modules. Basically, I want to be able to log from anywhere with a simple Logger::info("Something").
The current application layout is like this:
root/
module/
module1/
src/
module2/
src/
module3/
src/
Where would be the best place to put this new logger class? And how to make sure it is accessible from any of the sub-modules?
I would suggest using the ServiceManager rather than calling a static method, this will allow you to more easily swap out the logging class if needed going forward.
Also ZF2 modules are quite flexible in terms of how code can be structured, so you could simply use:
module/Application/src/Application/Logger/LoggerFactory.php
Or perhaps create a Logger module if you see this code being reused in other projects.
I did see a blog post from Rob Allen yesterday regarding directory structure of modules - http://akrabat.com/zend-framework-2/thoughts-on-module-directory-structure/ - it may be worth having a quick read.
I put general classes that should be acceible by all under module/Application/src/WhateverName
For example, I put the auth class under:
module/Application/src/Application/Authentication/AuthenticationService.php
With the namespace:
namespace Application\Authentication;
And then I define the service in the Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'auth_service' => function ($sm) {
$zfauthservice = new \Zend\Authentication\AuthenticationService;
return new \Application\Authentication\AuthenticationService($zfauthservice, $sm->get('bp_user_mapper_model'));
},
);
}

extending native session class codeigniter 1.7.2

I'm using native session library to replace the built in session library in CI. I need to extend the class but when I drop in MY_Session.php, CI reverts back to the old /system/libraries/Session.php.
How to I extend a class that's replaced a core CI class like Session.php?
Simply by naming your class files identically to a native library will
cause CodeIgniter to use it instead of the native one. To use this
feature you must name the file and the class declaration exactly the
same as the native library. For example, to replace the native Email
library you'll create a file named application/libraries/Email.php
-user guide
then call it
class MY_Email extends CI_Email {
public function __construct()
{
parent::__construct();
}
}
Loading Your Sub-class:
$this->load->library('email');
EDIT
Try this:
Just load your new library (the one doing the extending):
Then, let's say we have Session.php and Mysession.php
<?php
load_class('session', false);
class Mysession extends Session {
//your code
}
You don't need the MY_ name tag still, I think you want to reserve that for it's original intended purpose to avoid confusion.
.. else just use an include() or require() :P

Model Helper Zend

from what i know there is only action helper & view helper available at zend framework.
is there any model helper?
or how we can implement the model helper?
There's nothing in ZF actually called a Model helper - but if your model is accessing a database table you might want to create it as a class which extends Zend_Db_Table_Abstract. See examples in the ZF manual: http://framework.zend.com/manual/en/zend.db.table.html
In Zend Framework there is nothing defined for the models helpers like there is for the views helpers, however you can work around it and still preserve the application design (avoid using the library folder).
The way I do it is by adding a Helper folder inside the models one. Then you have to name your class like this:
class Application_Model_Helper_DateHelper
{
...
}
Then the autoloader will take care of finding it and loading it.
Unfortunately this naming is a little different from how you do it in the views folder. In the views folder you can create a folder named helpers and use a naming convention like:
class Zend_View_Helper_DarkBlueMenu extends Zend_View_Helper_Abstract
{
...
}
However, if you name the folder inside models as helpers then the classes inside it have to be named like this:
class Application_Model_helpers_DateHelper
{
...
}
First I add this line in my configuration file ie. application.ini:
includePaths.library = APPLICATION_PATH "/../library"
Then I add a class
class App_Model_Helper {
public static function resultAggregation($results) {}
//.... all the helper you need
}
in a file placed in ..library\App\Model\Helper.php
This is the only way I found to factor the code I use in the model.
The helper method can then be called from the model:
App_Model_Helper::resultAggregation($results);
I am aware this breaks the OOD, so if any one has a better and cleaner solution I would greatly appreciate.

How Do You Create Multiple Instances of a Library Class in CodeIgniter?

I'd like to create several instances of a class in CodeIgniter. I have created my class as a library, but cannot figure out the syntax to use to create more than one instance.
From the CodeIgniter users guide:
CI Users Guide: Loader Class
Assigning a Library to a different object name
If the third (optional) parameter is
blank, the library will usually be
assigned to an object with the same
name as the library. For example, if
the library is named Session, it will
be assigned to a variable named
$this->session.
If you prefer to set your own class
names you can pass its value to the
third parameter:
$this->load->library('session', '',
'my_session');
Session class is now accessed.
using:
$this->my_session
I think that's what you're looking for.
I know this thread is long passed, but it was one of the questions I came across while looking for my answer. So here's my solution...
It's PHP. Create your class as a library, load it using the standard CI Loader Class, but use it like you would in a regular PHP script.
Build your class:
class My_class {
var $number;
public function __construct($given_number){
$number = $given_number;
}
public function set_new_num($given_number){
$number = $given_number;
}
}
Load it:
// This will load the code so PHP can create an instance of the class
$this->load->library('My_class');
Then instantiate and use the object where needed:
$num = new My_class(24);
echo $num->number;
// OUTPUT: 24
$num->set_new_num(12);
echo $num->number;
// OUTPUT: 12
The only time I use $this->my_class is to make calls to static functions that I code.
Sorry for reviving this topic but I think I might have something reasonable to add.
You can do this to add multiple instances of a class. I don't know if it violates Codeigniter standard usage anyhow but seems more Codeigniterish than loading a library (which creates $this->library_name which isn't used) and then making 2 MORE instances with the "new" keyword.
$this->load->library( 'my_library', '', 'instance1' );
$this->load->library( 'my_library', '', 'instance2' );
$this->instance1->my_class_variable = 1;
$this->instance2->my_class_variable = 2;
echo $this->instance1->my_class_variable; // outputs 1
echo $this->instance2->my_class_variable; // outputs 2
I use this in my code to generate different menus. I have a "menu" class and different instances for each menu, with different menu items in each.