Hi there i am creating some classes and methods for my project so that it can be used over and over
now the problem is this that is has lots of functions in these dlls. i want to give them some name as alias name so that user can easily find and call these function as per their requirements ..
is there any way to call a function with two or more name . if any one please give me ans....
Thanks
In what language?
In C#, creating an alias to a method is as easy as:
public class Demo
{
public int SomeMethodHere(string argument)
{
// Code here.
}
public int AliasToSomeMethod(string argument)
{
return this.SomeMethodHere(argument);
}
}
Do the functions need to be overridable?
If not just have another function call the alias that calls the main function.
Related
I'm really sick of typing $this->database or $this->otherVarOrFunc every time I call something in my controller/model/wherever else. Is there some sort of OOP trick to have for example $database instead of $this->database in every function of my controller?
I have well defined structure of BaseController on MVC level and BaseObject for everything else class related. These two contains 5 to 20 object variables (depends on app size ) and I would be much more satisfied if typing $this-> wasnt required.
Does it have negative impact on overall performance? Thanks in advance
At the beginning of each class function, you could define a local variable for the function that references to the $this.For example:
class a {
function __construct() {
$this->aa = "hey";
}
public $aa;
public function aaa() {
$ab = &$this->aa;
$this->aa = "hey1";
echo $ab;
}
}
$b = new a;
$b->aaa();
Now throughout the function, use $ab instead of $this->aa
I have Model_Group that extends ORM.
I have Controller_Group that gets a new ORM:
public function before()
{
global $orm_group;
$orm_group = ORM::factory('Group');
}
...and it has various methods that use it to get different subsets of data, such as...
public function action_get_by_type()
{
global $orm_group;
$type = $this->request->param('type');
$result = $orm_group->where('type', '=', $type)->find_all();
}
Then I have another controller (in a separate module) that I want to use to manipulate the object and call the relevant view. Let's call it Controller_Pages.
$orm_object = // Get the $result from Controller_Group somehow!
$this->template->content = View::factory( 'page1' )
->set('orm_object', $orm_object)
What is the best way to pass the ORM object from Controller_Group to Controller_Pages? Is this a good idea? If not, why not, and what better way is there of doing it?
The reason for separating them out into different controllers is because I want to be able to re-use the methods in Controller_Group from other modules. Each module may want to deal with the object in a different way.
This is the way I would do it, but first I would like to note that you shouldn't use global in this context.
If you want to set your ORM model in the before function, just make a variable in your controller and add it like this.
public function before()
{
$this->orm_group = ORM::factory('type');
}
In your Model your should also add the functions to access data and keep the controllers as small as possible. You ORM model could look something like this.
public class Model_Group extends ORM {
//All your other code
public function get_by_type($type)
{
return $this->where('type', '=', $type)->find_all();
}
}
Than in your controllers you can do something like this.
public function action_index()
{
$type = $this->request->param('type');
$result = $this->orm_group->get_by_type($type);
}
I hope this helps.
I always create an helper class for stuff like this
Class Grouphelper{
public static function getGroupByType($type){
return ORM::factory('Group')->where('type','=',$type)->find_all();
}
}
Now you're been able to get the groups by type where you want:
Grouphelper::getGroupByType($type);
Sorry if I am using wrong keyword. Might be I did not get sufficient information due to wrong keyword.
we create widget in Yii by this way:
class streamList extends CWidget {
//do some stuff
}
Also, we use this widget anywhere as
$this->widget("application.components.widget.streamList");
How can we write the widget in such a way that it accepts parameter(s) as
$this->widget("application.components.widget.streamList",array('title'=>'Posts');
I googled but did not solve. Please help, thank in advance.
Edit log:
Also, how can we define default parameter value to 'titile'? I tried public $title = Shk::getTitle(), but it did not work.
Use
class StreamList extends CWidget {
//do some stuff
public $title;
}
Any attributes can be initialized with default values and overwritten by
$this->widget("application.components.widget.StreamList",array('title'=>'Posts',.....)
EDIT
You can't initialize class attributes with functions. An explanation is given here. An option is to check whether $title is set and if not set it to Shk::getTitle() in the init() method like
public function init(){
....
if(!isset($this->title) || !$this->title)
$this->title=Shk::getTitle();
....
}
P.S for consistency it's better to Capitalize your class names.
I know how to create a class the will allow me to instantiate it and use across my project. What I want to be able to do is have functions without instantiating classes. For example, I know how to do this:
$core = new core();
$val = $core->convertToMyNotation($anotherval);
But what I want is to be able to do this ANYWHERE in any view, class whatever:
$val = convertToMyNotation($anotherval);
Where would I place these functions in order to be able to do that?
best way to do it, create a public function in components/Controller.php
public function globalFunction(){
// do something here.
}
and access it anywhere by
$this->globalFunction();
You can define a static method as an option.
class core{
public static function convertToMyNotation($value){
//do whatever here
return $value;
}
}
Then call it like so:
$val = core::convertToMyNotation($anotherval);
This requires no instantiation of the object to use. The only restriction is that you cannot use the $this property inside a static method.
Alternately, just define a file with your functions in it and include the file at some point early like, like within the boostrap script in your public_html/index.php file.
Edit: darkheir makes some good suggestions. Include such a class in your protected/components folder, and have it extend CComponent to gain some potentially useful enhancements.
By including the class in the protected/components folder, you gain the advantage of autoloading the class, by default.
There is no definitive question of your answer, it depends a lot on what the function will be doing!
If the function is performing some things specific to a model
(getting the last users, ...) this has to be in the User model as
Willem Renzema described:
class theModelClass {
public static function convertToMyNotation($value){
//do whatever here
return $value;
}
}
And you'll call it like
$val = theModelClass::convertToMyNotation($anotherval);
If the function is handling user inputs (sanitizing he inputs,
checking the values, ...) then it has to go to the controller and
you'll use Hemc solution:
Create a public function in components/Controller.php
public function globalFunction(){
// do something here.
}
and access it anywhere by
$this->globalFunction();
If the function is an Helper: performing some actions that do not
depend on models or user inoput then you can create a new class that
you'll put in your component directory:
class core extends CComponent{
public static function convertToMyNotation($value){
//do whatever here
return $value;
}
}
And
$val = core::convertToMyNotation($anotherval);
Actually, I think you're looking for this answer instead:
http://www.yiiframework.com/wiki/31/use-shortcut-functions-to-reduce-typing/
In essence, in your entry script, before you load up Yii, include a global functions file:
require('path/to/globals.php');
Then, any function defined in that file can be used as a shortcut. Be careful, but enjoy the power! :-)
Create something like
Class Core extends CApplicationComponent{
public function doSomething(){}
}
and in config main.php
'components'=>array(
'core'=>array(
'class' => 'Core'
),
),
and now you can call whenever you want
Yii::app()->core->doSomething();
I want to create a variable(possible global variable) in one action script file and want to use the same variable across all other action script files in the project. How to create such a variable and how to use the same variable across all .as files??
One simple way is to define a static variable in a Class (either create a new Class or use one of your existing classes):
// in MyConfig.as
class MyConfig {
static var myVariable:String = "Hi";
}
// You can access / set the value from any class using MyConfig.myVariable
trace(MyConfig.myVariable); // prints Hi
MyConfig.myVariable = "Hello";
trace(MyConfig.myVariable); // prints Hello
Create one public class (lets assume GlobalVariables.as)
No need to add its instance.
Now declare all the variables that you want to use across multiple classes as STATIC.
(you can also declare and create instances of classes in that class to avoid multiple instances of classes)
Also you can add common methods to this class
So whenever you want to access that variable declared in GlobalVariables class
you need to access using reference e.g GlobalVariables.variableName
sample code:
package classes{
public class GlobalVariables{
public static var strURL:String;
public static function setExternalLinks(){
strURL = "http://demourl.asmx/";
}
}
}