Yii2 generic behaviour in all models - yii2

How Can I add a generic behaviour function that would work for all models. I have added this code in all models. Now I don't want to repeat the same function in all models.

You can create a CustomModel class that extends ActiveRecord. In CustomModel, add the method that you want:
class CustomModel extends \yii\db\ActiveRecord {
public function custom_function() {
//Function details
}
}
Then make your models extend CustomModel
class Book extends CustomModel {
}
That way you can use this method in all your models that extends CustomModel without having to implement this method in each of them.

Related

LibGDX: addActor(Body body)

I get a little bit confused by the tutorial at this site:
http://williammora.com/a-running-game-with-libgdx-part-2/
Why is it possible to give a body to the addActor method?
Can someone explain me this?
I thought I must give it some Actor.
private void setUpGround() {
ground = new Ground(WorldUtils.createGround(world));
addActor(ground);
}
private void setUpRunner() {
runner = new Runner(WorldUtils.createRunner(world));
addActor(runner);
}
Take again a look at the code. There is nowhere passed a Body object to the addActor method.
The only objects I see passed as parameters to the addActor method are runner & ground.
But those classes are extending the Actor class and not Body, see the code:
public class Runner extends GameActor { //..
and
public class Ground extends GameActor { //..
last not least the author of the code has defined the GameActor class like this:
public abstract class GameActor extends Actor { //..
==> you can see that those are subclasses of Actor and not Body. I hope it is clearer now.
BTW: if you use development environments like eclipse you can make use of the "Type Hierarchy" view!

Display function in header.tpl - Prestashop

I would like to say that i am quite new to prestashop.
I have added FrontController.php to override/classes/controller.
The file contains the following code
class FrontControllerCore extends Controller
{
public function thefunct()
{
return 'AA';
}
}
Now in the header.tpl i try to call the function by using {FrontController::thefunct()}.
When I put the public function in classes/controller it works, but when I put it in the override folder it doesn't.
How do I display the function in my header.tpl?
And how does the override folder work then?
(I assume you use ps 1.5)
If you are overriding FrontController you need to initiate your class like so
class FrontController extends FrontControllerCore
and delete cache/class_index.php file, (I mean, delete it everytime you override new controller or class)
Teach yourself how to override in ps. through excellent docs.
EDIT:
class FrontController extends FrontControllerCore {
//here you should create and assign variables which you want to use in templates
public function initContent()
{
parent::initContent();
//in such a way you assign variables into smarty template
$this->context->smarty->assign(
array(
'acme_variable' => $this->acme()
)
);
}
protected function acme()
{
return "Wile E. Coyote and The Road Runner";
}
}
//header.tpl
<div>{$acme_variable}</div>

AS3: Compiler bug with inner classes and interfaces?

For some irrelevant reasons I need a class:
that inherits (directly or not) from MovieClip.
that implements a particular interface (let's assume here that this interface is empty since it does not change anything to the issue).
and whose .as file declares internal classes.
The following code sums this up:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip implements EmptyInterface { }
}
class MyInnerClass { }
The problem with that code above is that it will not always compile. As soon as I use MyClass as Linkage for one of my library's item the compiler complains about MyClass not being a subclass of MovieClip. On the other hand, everything works great if I instantiate it manually and add it to the stage.
It looks like the interface and the inner class are somehow mutually exclusive in that very particular case. Indeed, if I remove the inner class I do not have that error anymore:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip implements EmptyInterface { }
}
Same thing when I remove the implemented interface but keep the inner class:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip { }
}
class MyInnerClass { }
Note that I've only tested this in Flash CS5.
I say it is a bug of compiler.
I have tested and found that private class must extend any class that is not Object. Instead of extending class it can also implement any interface.
This works same even if I put classes into deeper package.
I have tested this with Flash CS6.
If I'm reading you right, you want a public class to extend an internal class? - There is nothing that prevents you from doing this, so long as you declare your internal class as it's own packaged file.
According to the documentation:
[dynamic] [public | internal] [final] class className [ extends superClass ] [ implements interfaceName[, interfaceName... ] ] {
// class definition here
}
If it's the interface that is giving you grief, have you declared it in a separate file as well - that you would import? As eluded to in the comments, the namespace scoping is important so that the compiler understands what the escalating priority is.
Eg:
package my.example {
public interface EmptyInterface
{
}
}
So that:
package {
import flash.display.MovieClip;
import my.example.EmptyInterface;
public class MyClass extends MovieClip implements EmptyInterface { }
}
If this doesn't fix it I have another idea but try this first.
Click file
Click publish setting
Click on settings button
Uncheck Automatically declare stage instances
Click OK

Super interface and super class having the same method name

I am trying to create a spark datagrid item renderer. This item renderer extends a checkbox, and implements IGridItemRenderer
public class CellCheckBoxItemRenderer extends CheckBox implements IGridItemRenderer
When I implement IGridItemRenderer, I need to implement the interface methods, I am having a problem with the following methods:
public function get hovered():Boolean
{
}
public function set hovered(value:Boolean):void
{
}
since the methods are inherited as well from the checkbox
EDIT
The signatures of the functions
//spark checkbox signature
protected function get hovered():Boolean
protected function set hovered(value:Boolean):void
and the signature above belongs to the interface IGridItemRenderer
I guess the implementation of IGridItemRenderer is the more important part, so you can use it in a datagrid. The CheckBox provides just the functionality, you don't have to extend it if there are conflicts in my opinion.
public class CellCheckBoxItemRenderer implements IGridItemRenderer {
private var checkBox:CheckBox;
public function getCheckBox {
return checkBox;
}
//...
}
If CheckBox would implement any useful interfaces, you could also implement them in your renderer and delegate the methods to the checkbox, which may let you encapsulate the whole checkbox. That's not the case here though.
The problem is that interfaces, by design, only specify the signature for public functions, whereas the function in Checkbox is set as protected.
The only solutions:
remove the interface/Checkbox class from CellCheckBoxItemRenderer
remove the declaration from the interface
change Checkbox so hovered is a public property
it might be possible to change the accessor dynamically using the as3 commons bytecode project (http://www.as3commons.org/as3-commons-bytecode/emit.html), but I'm not 100% sure.

Language Agnostic Basic Programming Question

This is very basic question from programming point of view but as I am in learning phase, I thought I would better ask this question rather than having a misunderstanding or narrow knowledge about the topic.
So do excuse me if somehow I mess it up.
Question:
Let's say I have class A,B,C and D now class A has some piece of code which I need to have in class B,C and D so I am extending class A in class B, class C, and class D
Now how can I access the function of class A in other classes, do I need to create an object of class A and than access the function of class A or as am extending A in other classes than I can internally call the function using this parameter.
If possible I would really appreciate if someone can explain this concept with code sample explaining how the logic flows.
Note
Example in Java, PHP and .Net would be appreciated.
Let's forget about C and D because they are the same as B. If class B extends class A, then objects of type B are also objects of type A. Whenever you create an object of type B you are also creating an object of type A. It should have access to all of the methods and data in A (except those marked as private, if your language supports access modifiers) and they can be referred to directly. If B overrides some functionality of A, then usually the language provides a facility to call the base class implementation (base.Foo() or some such).
Inheritance Example: C#
public class A
{
public void Foo() { }
public virtual void Baz() { }
}
public class B : A // B extends A
{
public void Bar()
{
this.Foo(); // Foo comes from A
}
public override void Baz() // a new Baz
{
base.Baz(); // A's Baz
this.Bar(); // more stuff
}
}
If, on the other hand, you have used composition instead of inheritance and B contains an instance of A as a class variable, then you would need to create an object of A and reference it's (public) functionality through that instance.
Composition Example: C#
public class B // use A from above
{
private A MyA { get; set; }
public B()
{
this.MyA = new A();
}
public void Bar()
{
this.MyA.Foo(); // call MyA's Foo()
}
}
depending on the access level (would be protected or public in .NET), you can use something like:
base.method(argumentlist);
the base keyword in my example is specific to C#
there is no need for an instance of class A, because you already have a class A inherited instance
Basically you need a reference to the parent class.
In PHP:
parent::example();
From: http://www.php.net/manual/en/keyword.parent.php
<?php
class A {
function example() {
echo "I am A::example() and provide basic functionality.<br />\n";
}
}
class B extends A {
function example() {
echo "I am B::example() and provide additional functionality.<br />\n";
parent::example();
}
}
$b = new B;
// This will call B::example(), which will in turn call A::example().
$b->example();
?>
I find that the best way to tame the complexity of inheritance is to ensure that I only make B inherit from A when it really is a specialization of the superclass. At that point, I can call A's methods from inside B just as if they were B's own methods, and if B has overridden them then I can only suppose that this must be for a good reason.
Of course, quite often it is useful for B's implementation of a method to invoke A's implementation on the same object, generally because the subclass is wrapping extra behavior around the superclass's basic definition. The way in which you do this varies between languages; for example, in Java you do this:
super.methodName(arg1, ...);
Here's a quick Java example:
public class Aclass
{
public static void list_examples()
{
return("A + B = C");
}
}
public class Bclass extends Aclass
{
public static void main(String [] args)
{
System.out.println("Example of inheritance "+list_examples);
}
}
Note that the method for accessing the parent class shouldn't change. Because you are extending you shouldn't have to say parent:: or anything unless you are overriding the parent method / function.
It seems to me that extending your class might not be your best option. Class "B", "C", and "D" should only extend class "A" if they are truly an extension of that class, not just to access some method. For instance "Huffy" should not extend "BrandNames" just because "Huffy" is a brand name and you want access to one of the methods of "BrandNames". "Huffy" should instead extend "Bicycle" and implement an interface so the methods of "BrandNames" can be used. An additional benefit here is that (in Java) multiple interfaces can be used but a class can only be extended once. If in your example class "B"' needed to access a method from class "A" that could work, but if class "C" needed to access a method from class "A" and class "'B"' then you would have to use an interface in class "'C".