Yii2. Override module class - yii2

Is it possible to override module Module.php class to add some additional configs? I've extended some views and controllers and want to add a few additional configs for them.

you could create your own module in a separated vendor dir and extend the module you preferer
the sample in your image is already an ovveride (is an extension od BaseModule in this case) that redefine some function, add new param .. and os you
namespace dektrium\user;
use yii\base\Module as BaseModule;
class Module extends BaseModule
{
so in eg: myvendorname you could define a new Module.php
with
namespace myvendorname\user;
use dektrium\user\Module as MyBaseModule;
class Module extends MyBaseModule
{
// redefine your params and functions
// add your new params and function
You can take a look a this for a brief guide and suggestion
http://www.yiiframework.com/doc-2.0/guide-structure-modules.html
http://www.yiiframework.com/doc-2.0/yii-base-module.html

Related

How do I override a widget method in Yii2?

I need to override the renderPageButton() method in the Yii2 LinkPager widget. The method documentation specifically says "You may override this method to customize the generation of page buttons" but I can't figure out how to do that. Thanks.
Overriding LinkPager can be done this way:
Create a new file ./widgets/MyLinkPager.php:
<?php
namespace app\widgets;
use yii\widgets\LinkPager;
class MyLinkPager extends LinkPager
{
protected function renderPageButtons()
{
// do whatever you want, it may help to
// copy code from parent::renderPageButtons()
// or even call
return parent::renderPageButtons();
}
}
And then use it this way in your view (see here: https://www.yiiframework.com/doc/guide/2.0/en/output-pagination):
use yii\widgets\LinkPager;
echo LinkPager::widget([
'pagination' => $pagination,
]);
The class you want to override is documented here.
You can override it in the following way:
Create a new directory in your yii2 app root folder, like widgets
Create a new php file (like MyLinkPager.php) and a new class in it (MyLinkPager) which extens yii\widgets\LinkPager
You can use "app\widgets" namespace (i.e. if you are working with the basic yii2 app)
In your class, implement only the function you want to override from the original class
Use your new class wherever you want instead of the original one

In PhpStorm, can Ctrl+Click go to class definition in PHP rather than the constructor?

I'm using PhpStorm and something that lately is bothering me a lot is this scenario. Suppose I have this setup:
// File1.php
abstract class AbstractBase {
public function __construct() {
}
}
// File2.php
class MyClass extends AbstractBase {
}
// File3.php
$var = new MyClass();
Now, if I'm reading the line if File3.php and want to go to MyClass definition if File2.php, the easiest way is to hold the Ctrl button and then click the MyClass name. This works very nicely for functions and member variables, and in PHPDoc comments, but in this case PhpStorm chooses to go to the constructor rather than the class definition. And since MyClass doesn't have a constructor of its own, it goes to the AbstractBase constructor in a completely different file.
I understand that in this case it's ambiguous what I want to do. I also know that I can right-click MyClass and then select Go To and then Type Declaration. But is there a way to configure Ctrl-Click that it would go to the class definition rather the constructor?

Call function in indexController from another controller Zend

Need to call some function from another controller (not IndexController), for example:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$someFunction = new CustomController();
$someFunction->someFunc();
}
}
But this throws an error :
Fatal error: Class 'CustomController' not found in C:\xampp\htdocs\demo.ru\application\controllers\IndexController.php on line 13
If YourController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class,
so that both controllers can access the shared functionality without having to depend on each other.
I would suggest you to follow DRY and move those functions to common library place.
to use with Namespace see
Zend Framework calling another Controller Action
hope this will sure help you.

How Actionscript 3 Classes Work

I need a little help understanding how classes work in Actionscript 3. I understand you start with "package" and why and then go to import any necessary libraries, as well as then naming the class and stating if it's public/private and extends anything.
After that is what I don't understand. It seems you write "(public) function class name()
I don't understand why you do this and what goes in the curly brackets.
I've probably missed a bit of earlier reading because I've done a little reading but I can't seem to get it.
Could someone try explain it to me? Thanks.
ActionScript 3 Classes
The package statement.
Okay, so firstly like you mentioned, a class must be wrapped by a package1. This gives us the first block, where you need to define the class.
package
{
// Your class here.
}
The package statement reflects the location of the class relative to the .fla2. For example, if you have a folder "classes" within the same directory as the project .fla, then classes within that folder will need a package statement that reflects that:
package classes
{
// Your class here.
}
Defining the class.
Within a package statement, you may insert one class. Do not confuse this with the package itself, which can contain many classes - each class just needs to have its own file with the same package statement.
A class definition is made up of up to 5 parts:
The namespace. A class can be internal or public. An internal class can only be seen by classes within the same package, whereas public classes can be seen from anywhere in the project.
The class name.
A base class (optional). If a base class is defined, then your new class will act as an extension to that class, inheriting all of the qualities of the base class.
An interface to implement (optional). Interfaces are an advanced topic thus I suggest you forget about these for now until your AS3 and OOP have evolved.
If you wanted to create a class called "Person" within the package classes, then we would end up with:
package classes
{
public class Person
{
// Class qualities here.
}
}
Properties.
Classes can contain properties. Properties are defined using the var keyword. They may belong to one of a number of namespaces (including your own) and are used to hold values that belong to your class. Properties are most commonly found clustered together at the top of your class.
Our Person class may enjoy the properties height and weight:
package classes
{
public class Person
{
// Properties.
public var height:Number = 1.70;
public var weight:Number = 67.5;
}
}
These properties can be accessed via any instance of Person that you create. Each instance will have its own set of these properties.
Class constructors (I believe this is what you're asking about).
Constructors are used to hold logic that should be run as soon as an instance of your class is created. The class constructor has the same name as the class itself. It must be public and it does not return anything. Constructors can accept arguments, which are typically used to pass in references to dependencies for that class or required values.
package classes
{
public class Person
{
// Properties.
public var height:Number = 1.70;
public var weight:Number = 67.5;
// Constructor.
public function Person(height:Number, weight:Number)
{
this.height = height;
this.weight = weight;
}
}
}
Methods.
Methods are used to hold logic that can be run when calling that method. Methods often return values and can accept arguments. Methods can belong to any namespace that you would expect properties to be able to belong to.
We may want to be able to easily determine the BMI of each instance of Person that we create, so we should create a method for that:
package classes
{
public class Person
{
// Properties.
public var height:Number = 170;
public var weight:Number = 65.5;
// Constructor.
public function Person(height:Number, weight:Number)
{
this.height = height;
this.weight = weight;
}
// Determine my BMI and return the result.
public function getBMI():Number
{
return weight / (height * height);
}
}
}
Instances.
Now that we've defined our new class, we can create instances of this class using the new keyword. This can be done from anywhere that can access the Person class, which in this case is anywhere in the project because we've made the class public.
Though the class is public, accessing it from anywhere outside of the package it belongs in will require the use of an import statement. This statement will need to be used within any class that belongs to a different package. The import statement follows the same name used for the package and includes the name of the class you want to include on the end:
import classes.Person;
Once you've imported Person, you can create instances of it and assign them to a variable with different height and weight values:
var marty:Person = new Person(71, 1.76);
var bruce:Person = new Person(96.4, 1.72);
We can then obtain the BMI for each person using their getBMI() method:
trace(marty.getBMI()); // 22.9
trace(bruce.getBMI()); // 32.6
1 You can place classes outside of a package which can be referred to in the same .as file.
2 You can add more source paths, and packages can be relative to that.
The function that have the same name as class is a constructor. In curly brackets is basically part of code that will execute instantly when object will be created. Try to search info about constructors, they exist I think in every object oriented programming language (I may be wrong), so you have a lot of resources.
You can also read about this concept on Wikipedia.
The function that is named the same as the class is the constructor. It's optional, so you can leave it out if you don't need it. A default constructor will be added, which essentially does nothing.
The constructor lets you write code that executes immediately after an instance of the class is created (ie when another bit of code runs new ClassName(). You would typically use it to initialise some variables that are used by the class. Defining a constructor also lets you handle constructor arguments, which other code can pass when they use the new operator.

AS3 - extend a function?

I have an Entity class with a destroy() function.
I also have an Enemy class that extends Entity, and I want to add some lines to the destroy() function.
Is there a way to extend functions in ActionScript 3, or is copy and paste the way to go? Thanks.
You need to mark the method with the override keyword, and from there use the same namespace (public, protected, etc) and name that make up the method you want to override in the class you're extending.
The method must also have the same return type and accept the same arguments
Sample override:
override public function destroy():void
{
// add more code
super.destroy();
}
If you exclude the line which reads super.destroy(), the function within the base class will not be run, and only your new code will be used instead.