How to access a function from the library class file in the magento2 custom controller

By : Rahul

I was trying to create an extension for redis in magento 2.For that i was trying to call getTag() function from magento2 Redis.php lib\internal\cm\cache\backend\Redis.php in my custom controller.

Let me know how can i access the function from the library class lib\internal\cm\cache\backend\Redis.php in my custom controller. Can anyone show me some examples of accessing a library function in the magento2 custom controller.

Thanks in advance

By : Rahul


You can call it directly

$obj = new \Cm_Cache_Backend_Redis($options = array());

You need to specify the options.

I would suggest the following general design pattern. It wastes an additional pointer, but will enforce the requirement that the const object will be able to only access const methods of the private object:

class ObjectImpl;

class const_Object {


  const_Object(const ObjectImpl* object_impl)
    : _impl(object_impl);

  // Only const methods

  const ObjectImpl* _impl;

class Object : public const_Object
  Object(ObjectImpl* object_impl)
    : const_Object(object_impl), _impl(object_impl);

  // non-const methods go here.

  ObjectImpl* _impl;

Adding to Ian's answer, Any complex element that is named by root element will have factory method annotated with @XmlElementDecl().

You can resolve this, by moving the complex type declaration inline like below.

<xsd:schema xmlns= "" xmlns:xsd=""
    xmlns:annox="" xmlns:jaxb=""
    targetNamespace="" jaxb:extensionBindingPrefixes="annox"
    jaxb:version="2.1" elementFormDefault="qualified">
    <xsd:element name="unit">
                <xsd:any processContents="skip" />

ObjectFactory.class (no JAXBElement factory method generated here)

public class ObjectFactory {

     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package:
    public ObjectFactory() {

     * Create an instance of {@link Unit }
    public Unit createUnit() {
        return new Unit();


Test class:

public void testUnmarshalling() throws JAXBException, SAXException {
    JAXBContext context = JAXBContext.newInstance(Unit.class);

    Unmarshaller unmarshaller = context.createUnmarshaller();

            .newSchema(new StreamSource(getClass().getClassLoader().getResourceAsStream("common.xsd"))));

    Object unit = unmarshaller.unmarshal(getClass().getResourceAsStream("unit.xml"));

    System.out.println(">>> Class is: " + unit.getClass().getName());

    StringWriter writer = new StringWriter();
    context.createMarshaller().marshal(unit, writer);

    System.out.println(">>> XML is: " + writer.toString());

    //assertTrue(unit instanceof Unit);

Test xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<unit xmlns="">

output :

>>> Class is:
>>> XML is: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><unit xmlns=""><text>Name</text></unit>
By : ulab

This video can help you solving your question :)
By: admin