What is Super Object in CodeIgniter? - oop

I saw the term "Super Object" in CodeIgniter manual, but the term is not explained in details.
So, what is exactly "super object" in CodeIgnter?

The codeigniter super object is the object that lets you refrence any loaded codeigniter resource or load new ones without initializing the classes each time.
for instance in your library if you wanted to refrence the database you would do the following
function whatever()
{
$this->ci =& get_instance() // sets an object in your library to point to the codeigniter object
$this->ci->db->get('mytable');
}
where in a controller it would just be
function whatever
{
$this->db->get('mytable);
}
this is because libraries do not have a refrence to the codeigniter object by default (for many reasons)

Related

How to iterate Apache velocity template variable attributes

As title, is there any way to iterate or display Apache velocity template attributes?
for example, I have following code :
<code>
${ctx.messages.headerMessage}
</code>
And I want to know how many other attributes the variable ${ctx} has
It's only possible to discover and to loop on an object properties (that is, the ones with getters and/or setters) if you can add a new tool to your Velocity context. If you can't, you're rather stuck.
There are several ways to do this, I illustrate below how to do this with commons-beanutils.
First, add Apache commons-beanutils in your class path, and add it to your Velocity context from Java:
import org.apache.commons.beanutils.PropertyUtils;
...
context.put("beans", new PropertyUtils());
...
One remark: if you do not have access to the Java part, but if by chance commons-beanutils is already in the classpath, there is one hakish way of having access to it: #set($beans = $foo.class.forName('org.apache.commons.beanutils.PropertyUtils').newInstance()).
Then, let's say that I have the following object:
class Foo
{
public boolean isSomething() { return true; }
public String getName() { return "Nestor"; }
}
which is present in my context under $foo. Using your newly $beans properties introspector, you can do:
#set ($properties = $beans.getPropertyDescriptors($foo.class))
#foreach ($property in $properties)
$property.name ($property.propertyType) = $property.readMethod.invoke($foo)
#end
This will produce:
bar (boolean) = true
class (class java.lang.Class) = class Foo
name (class java.lang.String) = Robert
(you'll need to filter out the class property, of course)
One last remark, though. Templates are for coding the View layer of an MVC application, and doing such a generic introspection of objects in them is rather inadequate in the view layer. You're far better of moving all this introspection code on the Java side.

Swift 2: use extensions to turn an Apple class into a singleton?

I have a lot of different classes that need to access the same GKMatch object, and I'm getting wary of how often I have to pass that GKMatch reference around. I want to find a way that any class can reliably get the same GKMatch instance.
I am proposing doing it indirectly via GKMatchDelegate, like this:
extension GKMatchDelegate {
static func sharedGKMatch ()->GKMatch? { return nil }
}
Then, in my GKMatchDelegate subclass, I would create a currentMatch: GKMatch? variable and override the function to return it.
When I tried to test the concept in a playground, I did this:
class SingletonTest {}
extension SingletonTest {
static func shared ()->SingletonTest? { return nil }
}
class SubclassOfSingletonTest: SingletonTest {
static var sharedTest: SingletonTest?
override static func shared()->SingletonTest? {
return sharedTest
}
}
And got the error message "Declarations from extensions cannot be overriden yet". From the "yet" I'm guessing they're working on it. In the meantime, is there another way to achieve the same result?
I'm assuming you only want one shared GKMatch, not a GKMatch for every subclass of GKMatchDelegate. In the former case, I think you are overthinking this. Just have a public var that is the singleton not in any class.
public var sharedGKMatch: GKMatch = GKMatch()
If you want to make it read only
public let sharedGKMatch - GKMatch()
It's a class so the content of GKMatch is still mutable.
Most applications have some sort of data model -- an object or group of objects that stores all the information that the app needs to do whatever the app does. For a word processor, for example, the data model might be a collection of open documents together with any global configuration information (printer information, fonts, preferences, etc.). For a game, the model contains the current state of the game -- board state, current locations and velocities of bad guys, number of gold coins collected, whatever.
Your app should have a data model too, and it sounds like the current GKMatch instance should be included in that model. That way, any class that has access to the model automatically has access to the GKMatch, which means that you've only got one thing that you need to pass around. That's almost certainly a better approach than adding a shared object interface to someone else's class.

Ninject Factory - "new" object being passed in instead of one called in factory method

I am using the Ninject Factory Extensions so that I can create objects that have services injected plus custom values
so:
public interface IGameOperationsFactory
{
ISpinEvaluator Create(GameArtifact game);
IReelSpinner CreateSpinner(GameArtifact game);
}
Then in module:
Bind<IGameOperationsFactory>().ToFactory().InSingletonScope();
Bind<ISpinEvaluator>().To<DefaultSpinEvaluatorImpl>();
Bind<IReelSpinner>().To<DefaultReelSpinnerImpl>();
The actual factory gets injected in a classes' constructor and is then used like:
_spinner = _factory.CreateSpinner(_artifact);
_spinEval = _factory.Create(_artifact);
Where _artifact is of type GameArtifact
Then in each of the implementation's constructors services plus the passed in objects are injected. The GameArtifact is successfully passed in the first constructor, and in the second one a "new" GameArtifact is passed in, i.e. not a null one but one with just default values as if the framework just called
new GameArtifact()
instead of passing in the already existing one!
The Constructor for the two objects is very similar, but the one that doesn't work looks like:
[Inject]
public DefaultReelSpinnerImpl(GameArtifact ga, IGameOperationsFactory factory, IRandomService serv)
{
_rand = serv;
_ra = ga.Reels;
_mainReels = factory.Create(_ra);
_winLine = ga.Config.WinLine;
}
Where the factory and serv are injected by Ninject and ga is SUPPOSED to be passed in via the factory.
Anyone have a clue why a new "fresh" object is passed in rather than the one I passed in??
I have rewritten you sample a little bit, and it seems to work fine. Could you provide more detailed code sample?
My implementation
I have changed verb Create to Get to match Ninject conventions
public interface IGameOperationsFactory
{
ISpinEvaluator GetSpinEvaluator(GameArtifact gameArtifact);
IReelSpinner GetReelSpinner(GameArtifact gameArtifact);
}
Ninject configuration
I have added named bindings to configure factory
Bind<ISpinEvaluator>()
.To<DefaultSpinEvaluatorImpl>()
.Named("SpinEvaluator");
Bind<IReelSpinner>()
.To<DefaultReelSpinnerImpl>()
.Named("ReelSpinner");
Bind<IGameOperationsFactory>()
.ToFactory();
ps: full sample with tests

In MVC, is a controller an object or a function?

I am learning Symfony2, and part of the documentation on controllers states that the methods on the controller object are actually the controllers, whereas the object is more of a controller container.
Which part specifically is referred to as the controller(s)? I'm new to MVC and OOP, so I'm just trying to make sure I have it right.
the page describes actually a convention endorsed by Symfony2 creators.
in some MVC frameworks (esp. in Java) controllers are implemented by one-class-per-controller convention, e.g.:
class ListContactsController {
public function start() {
// query db...
return ...;
}
}
class AddContactController {
public function start($name, $details) {
// insert into db...
return ...;
}
}
note that every controller-class has one method start() that defines what the controller actually does
in other MVC frameworks (like Symfony2 or cake-php) controllers are implemented by one-method-per-controller convention, grouped together for convenience, e.g.:
class ContactsController {
public function list() {
// query db...
return ...;
}
public function add($name, $details) {
// insert into db...
return ...;
}
}
here the convention assumes every controller is implemented as a method rather than a separate class with a particular method like start()
EDIT: another way of thinking about this difference is this:
one-class-per-controller assumes there is one controller instance (might hold internal state) and when user interacts with the view, view is communicating with that controller instance via callbacks i.e. methods in controller's class.
one-method-per-controller assumes any state is contained within parameters that are passed to methods, and when user interacts with the view, view is communicating with separate controllers/actions. those controllers are seen as independent concepts.
In the example page you shared, the "class HelloController" is the Controller and its' functioned are Controller "Methods".
Okay, they are referring to the default method, indexAction() as the Controller.
In this MVC architecture (and most others, if not all) the "index" method is the default method (function) called when that controller is requested.

NHibernate lazy loading property - what does build-time bytecode instrumentation mean?

I've tried to lazy-load a property in my domain model, but lazy loading doesn't work. (It is always loaded).
[Property(0, Column = "picture", Lazy=true)]
public virtual System.Byte[] Picture
{
get { return picture; }
set { picture = value; }
}
When reading the documentation here it says that it requires build-time bytecode instrumentation. What does this mean - and how can I get it ?
I have you tried a collection rather then an array?
[Property(0, Column = "picture", Lazy=true)]
public virtual IList<System.Byte> Picture
{
get { return picture; }
set { picture = value; }
}
For lazy loading to work NHibernate makes use of interception (via dynamic objects). That means it wraps your call to Picture and when you first call Picture it it will load the property from the database.
For this to work it can use one of three types of Dynamic object frameworks:
Castle DynamicProxy
Linfu
Spring
When you download NHibernate there is another folder with three types of these dynamic object plugins and you need to copy three dlls to the nhibernate folder (where nhibernate.dll is) and set a property in your nhibernate configuration file.
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
Ref: http://nhforge.org/blogs/nhibernate/archive/2008/11/09/nh2-1-0-bytecode-providers.aspx
HTH Alex