CakePHP Manipulate more than one models in the same controller - php

I created two plugins in the CakePHP, and both of them have the same named model, e.g, plugin1.mod and plugin2.mod.
In the two models, defined the same named method, e.g, mymethod.
Now, I have a controller in my main program. Then, use ClassRegistry::init to initialize plugin1 and can call plugin1.mod without problem.
Problem:
When I use ClassRegistry::init to initialize plugin2 and call the plugin2.mod, it is calling plugin1.mod! Can somebody tell me what wrong here is?
Thank you.

in 2.0 this is not possible (anymore).
class paths are cached (inside App class) and therefore can use a class name only once.
You need to use different class names.

Related

PHP, how to effectively use the functions in a class

I am using codeigniter framework. let say i had controller class named myclass, this class have a function that i want to access from another controller class.
I got troubled when access the function from another controller class, i cant create an instant of this myclass class, it say class 'myclass' not found. i dont want to use include 'myclass.php'; because class myclass have __constructor method, i afraid the content inside __constructor method will conflict with another __constructor.
what the best solution for my case?
Thanks
CodeIgniter framework has a way to automatically loading your custom classes. Look here: http://www.codeigniter.com/userguide2/general/autoloader.html
Just make it possible for the framework to auto-load your class(es) and the problem is solved.

Calling static autoloaded class without namespace

I was wondering if it's possible to use a static PHP class (or a method of that class, should I say) without calling it via the namespace ala laravel?
I realise this is conflicting with the nature of namespaces, but I was mainly wanting to use them the same as Laravel does, where I can just call Example::method('test') rather than \example\example\Example::method('test')
At the top of your file, declare a use statement which creates an alias:
use example\example\Example;
Example::method('test'); //resolves to example\example\Example::('test')
You can even use a different name with 'as':
use example\example\Example as MyAlias;
MyAlias::method('test'); //resolves to example\example\Example::('test')
See http://php.net/manual/en/language.namespaces.importing.php for more information.
Note: This is not how laravel facades work but I think it is what you are looking for. If you want something more automatic then aliasing you will have to incorporate some logic into your autoloader.

The management of files and folders in codeIgniter can be made from controller or model?

I want to verify if file/folder exists on my CI system (not system folder), can i do it from controller or I need create a method on my model to do it?
It's basically a matter of choice. You can define your function in both your model and controller. But if you want to use it over and over from different controllers, then better define a new model and put that function in it. I'd define model which is to be used by many controllers that is having some common-general methods. Just to keep things apart and maintaining them later. Helpers are also a way to keep things separate.
You have got many way for this :
You can create an helper and put your function like verify()
You can create a private function into your controller like
private function _verify() { }
enjoy !
Best to add this function in a codeigniter helper or in a library class(if you are fan of OOP). Load this automatically with autoload.php and call from wherever you need it.

PHP workaround to extend classes of the same name?

I know extending a class with the same name is not possible, but I was curious if anyone knew of a way to load a class then rename it, so i can later extend it with the original name. Hopefully like something below:
<?php
//function to load and rename Class1 to Class2: does something like this exist?
load_and_rename_class('Class1', 'Class2');
//now i can extend the renamed class and use the original name:
class Class1 extends Class2{
}
?>
EDIT:
Well, I understand that this would be terrible practice in a basic OOP environment where there are large libraries of class files. But i'm using the CakePHP MVC framework and it would make great sense to be able to extend plugin classes in this way since the framework follows a well established naming convention (Model names, view names, controller names, url routes (http://site.com/users), etc).
As of now, to extend a CakePHP plugin (eg: Users plugin) you have to extend all the model, view, and controller classes each with different names by adding a prefix (like AppUsers) then do some more coding to rename the variable names, then you have to code the renamed url routes, etc. etc. to ultimately get back to a 'Users' name convention.
Since the MVC framework code is well organized it would easily make sense in the code if something like the above is able to be implemented.
I'm trying to work out why this would be necessary. I can only think of the following example:
In a context that you have no control over, an object is initialised:
// A class you can't change
class ImmutableClass {
private function __construct() {
$this->myObject = new AnotherImmutableClass();
}
}
$immutable = new ImmutableClass();
// And now you want to call a custom, currently non existing method on myObject
// Because for some reason you need the context that this instance provides
$immutable->myObject->yourCustomMethod();
And so now you want to add methods to AnotherImmutableClass without editing either Immutable class.
This is absolutely impossible.
All you can do from that context is to wrap that object in a decorator, or run a helper function, passing the object.
// Helper function
doSomethingToMyObject($immutable->myObject);
// Or decorator method
$myDecoratedObject = new objectDecorator($immutable->myObject);
$myDecoratedObject->doSomethingToMyObject();
Sorry if I got the wrong end of the stick.
For more information on decorators see this question:
how to implement a decorator in PHP?.
I happen to understand why you would want to do this, and have come up with a way to accomplish what the end goal is. For everyone else, this is an example of what the author may be dealing with...
Through out a CakePHP application you may have references to helper classes (as an example > $this->Form->input();)
Then at some point you may want to add something to that input() function, but still use the Form class name, because it is through out your application. At the same time though you don't want to rewrite the entire Form class, and instead just update small pieces of it. So given that requirement, the way to accomplish it is this...
You do have to copy the existing class out of the Cake core, but you do NOT make any changes to it, and then when ever you upgrade cake you simply make an exact copy to this new directory. (For example copy lib/Cake/View/Helper/FormHelper.php to app/View/Helper/CakeFormHelper.php)
You can then add a new file called app/View/Helper/FormHelper.php and have that FormHelper extend CakeFormHelper, ie.
App::uses('CakeFormHelper', 'View/Helper');
FormHelper extends CakeFormHelper {
// over write the individual pieces of the class here
}

PHP OOP - Require or Extend

I was wondering if there is any major different in the following, and whether one is more 'standard' than the other:
<?php
class Account extends Database {
public function myMethod()
{
// Do something
}
}
?>
or
<?php
require('database.class.php');
class Account {
public function myMethod()
{
// Do something
}
}
?>
Cheers :)
Edit:
This question actually relates to a tutorial series I have been following which describes the above two methods - which didn't make any clear sense.
So thank you for the constructive answers on clearing that one up!
Those are two completely separate language constructs.
Your first example deals with inheritance. Basically, you already have a class called Database, but you want to have a specialized version of that class to handle accounts. Rather than build a brand new Account class and copy/paste all the functionality you already have in your Database class, you simply tell PHP that you want to use the existing Database class as a baseline. You create any account-specific functionality in the new Account class, and anything database-related comes automatically. This is assuming, of course, that you have some way of specifying where the Database class is defined - for example, a require declaration at the top of the class, or an __autoload() or spl_autoload_register() function call defining a way to find and locate the file containing the Database class.
In your second example, your database-related code is completely separated from your Account class. They're completely distinct entities, and if you wanted to do anything database-related in your Account class, you would have to explicitly instantiate a new Database object within that class (or pass it to that class, or one of its functions, as a parameter.
Basically, extends helps define what a class is, whereas require shows where a class definition (or other code) is stored.
Both code snippets aren't even equivalent.
The first declares Account to extend Database, a is-a relation.
In the second code snippet, you are simply saying that you require 'database.class.php' ... and that neither has anything to do with OO, nor defines a is-relation from Account to Database.
Both are completely different in first one class is inherited by another class but in the second one the class is included in your script only.
Means if you extend all the public and protected methods are available in your derived class and you can create object of derived class and can use methods with derived class's object.
But in the second method the class is included in your script and require this class it's own method and work independently.
The first means you create a new class, which has all the functionality of Database class and those you implement.
The second means that you create a new class, but it doesn't have Database functionality since it's not extending it. If you need database access in your Account class, you can create an instance in constructor, or pass already created instance as constructor parameter.
It's hard to say what is more standard, since it depends on what You actually want to achieve.
To put it in most simple terms:-
require or include is structural programming.
extends is object oriented

Categories