i want to override AbstractBlock class, i tried with my custom module but it not working
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\View\Element\AbstractBlock" type="YourCompany\YourModule\Framework\View\Element\AbstractBlock" />
</config>
AbstractBlock.php
<?php
namespace YourCompany\YourModule\Framework\View\Element;
abstract class AbstractBlock extends \Magento\Framework\View\Element\AbstractBlock {
/**
* Retrieve child block HTML
*
* #param string $alias
* #param boolean $useCache
* #return string
*/
public function getChildHtml($alias = '', $useCache = true)
{
die("here");
}
}
You cannot replace a class in the middle of the hierarchy.
Meaning, exchanging an existing parent (abstract) class by another abstract class.
May be you can show how are you using the original class (I guess in a constructor) and that would help a little bit to give a better answer.
Related
I have defined an interface for an ordered list. The class docblock looks like this:
/**
* Interface ListOrderedInterface
* #template ListOrderedElement
*/
Within the method docblocks for this interface, ListOrderedElement is used to ensure that the type of thing being added to the list is consistent. PHPStan runs clean on ListOrderedInterface.php. So far so good.
Next I defined an interface for a factory that makes an ordered list. The definition looks like this:
/**
* Class ListFactoryInterface
*/
interface ListOrderedFactoryInterface
{
/**
* makeList
* #return ListOrderedInterface
*/
public function makeList(): ListOrderedInterface;
}
phpstan is complaining with the warning message "phpstan:: Method makeList return type with generic interface ListOrderedInterface does not specify its types". I don't know how to specify the types for the interface.
Thanks for helping with this.
You need provide a specialization for your ListOrderedInterface in the makeList phpdoc in the #return part.
interface ListOrderedFactoryInterface
{
/**
* This is an example with a concrete type, syntax is <type>
* #return ListOrderedInterface<string>
*/
public function makeList(): ListOrderedInterface;
}
If you need this to generic as well, you would need to add the #template on the factory also and return a generic type from makeList.
/**
* #template T
*/
interface ListOrderedFactoryInterface
{
/**
* #return ListOrderedInterface<T>
*/
public function makeList(): ListOrderedInterface;
}
And in the classes that implement FactoryInterface, add the #implements phpdoc.
/**
* Instead of string put the actual type it should return
* #implements ListOrderedFactoryInterface<string>
*/
class ConcreteFactory implements ListOrderedFactoryInterface
{
}
You can find more examples in the official docs: https://phpstan.org/blog/generics-by-examples
I'm using a single XML file to store configuration settings for multiple objects. For example:
<config>
<database>
<user>myuser</user>
</database>
<application>
<component1>mycomponent</component1>
</application>
<infrastructure>
<hostname1>server1</hostname1>
</infrastructure>
</config>
I want each of my objects to access different aspects of the xml; for instance the "database" class would access the database element.
What method could be used to quickly access this configuration across my objects without reproducing code?
You could create a Config object which reads the configuration from the XML file, then use a Configurable trait on the classes you want to be "automatically" configurable from the XML (see example below).
config.xml:
<config>
<database>
<user>myuser</user>
</database>
<application>
<component1>mycomponent</component1>
</application>
<infrastructure>
<hostname1>server1</hostname1>
</infrastructure>
</config>
PHP code:
class Config
{
/**
* Full XML configuration
*
* #var SimpleXMLElement
*/
private static $xml;
/**
* Parse XML configuration file into XML object (once)
*/
public function __construct()
{
if (!self::$xml) {
$xmlSource = file_get_contents('config.xml');
self::$xml = new SimpleXMLElement($xmlSource);
}
}
/**
* Returns the first node matching the specified xpath
*
* #param $xpath
*
* #return SimpleXMLElement
*/
public function getFirstByXpath($xpath)
{
// Return the first matching configuration
return self::$xml->xpath($xpath)[0];
}
}
trait Configurable
{
/**
* Class configuration XML element
*
* #var SimpleXMLElement
*/
private $configuration;
/**
* Get the class configuration XML element
*
* #return SimpleXMLElement
*/
function getConfiguration()
{
if (!$this->configuration) {
$tag = strtolower(get_class($this));
$xpath = '/config/' . $tag;
$this->configuration = (new Config())->getFirstByXpath($xpath);
}
return $this->configuration;
}
}
class application
{
use configurable;
}
class database
{
use configurable;
}
$app = new application();
echo "App component 1: ", $app->getConfiguration()->component1, PHP_EOL;
$db = new database();
echo "DB user: ", $db->getConfiguration()->user, PHP_EOL;
Output:
App component 1: mycomponent
DB user: myuser
Is there a way to automatically build a zend framework form by having a doctrine 2 entity-class?
Important Information: This is only a example and no productive setup.
E.g.
Users (Entity) class:
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*/
class Users
{
/**
* #var string
*/
private $name;
/**
* #var integer
*/
private $id;
/**
* Set name
*
* #param string $name
* #return Users
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
Users xml mapping definition:
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Users" table="users">
<id name="id" type="integer" column="ID">
<generator strategy="IDENTITY"/>
</id>
<field name="name" type="string" column="name" length="50" nullable="true"/>
</entity>
</doctrine-mapping>
Now i would like to have a zend 2 form class, where i can inject this entity and the form itself builds a basic form and attaches validators to this form. Here in this example it would atach a StringLenght validator (max 50; see xml mapping above) to the users field.
Something like this is possible like i know in CakePHP. I would be really happy to have such feature in Zend framework 2, too;-)
For now i found one link: http://www.lewanscheck.de/2013/09/21/inject-doctrine-orm-entitymanager-in-zf2-form/ but no automatic creation of validation from the xml file.
Would be great to implement only a model/entity class with a xml/yml/annotation(even when i do not like annotations) configuration and build automatically a db schema-part (this works with doctrine 2) and a form class.
Our classes don't have explictic getPropertyName() and setPropertyName() methods. Instead we're using trait, which implements combination of magic __get(), __set() and __call() methods, so, we can have following as result:
class Device {
use ModelAttributes;
/**
* #var \string
*/
protected $title;
/**
* #var \string
*/
protected $subtitle;
}
$device = new Device();
$device->setTitle('PC');
$device->getTitle();
But the problem is, that now PhpStorm can't recognize and highlights these methods.
I know, that I can add #method string getTitle() into class phpDoc, but this will mean, taht I again need to add all get-ers and set-ers, but in form of phpDoc commetns, and I want to avoid this.
It would be really great, if I could do something like:
/**
* #var \string
* #get
* #set
*/
protected $title;
Does someone have experience with such situation?
I just implemented namespaces in my small application as outlined here: http://www.yiiframework.com/doc/guide/1.1/en/basics.namespace
I'm running into an issue where my controller will no longer access Yii::app()->getRequest(); saying it can't find include(C:\Users\bkuhl\htdocs\instaLabel\application\protected\components\Yii.php): failed to open stream: No such file or directory.
I realize that's because I declared the namespace as application/components. But I'm not sure how to work around this one...
<?php
namespace application\components;
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class Controller extends \CController {
/* #var $request CHttpRequest */
protected $request = null;
/**
* #var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout='//layouts/column1';
/**
* #var array context menu items. This property will be assigned to {#link CMenu::items}.
*/
public $menu=array();
/**
* #var array the breadcrumbs of the current page. The value of this property will
* be assigned to {#link CBreadcrumbs::links}. Please refer to {#link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs=array();
public function __construct ($id, $module = null) {
parent::__construct($id, $module);
$this->request = Yii::app()->getRequest();
}
You need to fully qualify the relative class name Yii.
The most convenient way to do this is by importing the class: just add use Yii; below your namespace declaration.
Have you tried:
$this->request = \Yii::app()->getRequest();
\ will use the global namespace:
Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.