Tag Archives: Object

Object Prototypes JavaScript

All javascript objects inherit properties from a prototype.

For example,

Date objects inherit properties from the Date prototype

Math objects inherit properties from the Math prototype

Array objects inherit properties from the Array prototype.

On top of the chain is Object.prototype. Every prototype inherits properties and methods from the Object.prototype.

A prototype is a blueprint of an object. Prototype allows us to use properties and methods on an object even if the properties and methods do not exist on the current object.

Object Cloning

Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy.

An object copy is created by using the clone keyword (which calls the object’s __clone() method if possible). An object’s __clone() method cannot be called directly.

$copy_of_object = clone $object;

When an object is cloned, PHP 5 will perform a shallow copy of all of the object’s properties. Any properties that are references to other variables will remain references.

SplObjectStorage

The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.

  • It uses Objects are indexes
  • It can be used to implement set of objects
  • It allows arbitrary data to be associated with objects
  • It does not permit the serialization of objects

Magento 2 Factory Object

Factory Object is used to instantiate an object

The Factory class name is the name of Model class and append with the Factory word. So for our example, we will have TopicFactory class. You must not create this class. Magento will create it for you. Whenever Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist. You will see the factory class in

var/generation/<vendor_name>/<module_name>/Model/ClassFactory.php

To instantiate a model object we will use automatic constructor dependency injection to inject a factory object, then use factory object to instantiate the model object.

For example, we will call the model to get data in Block. We will create a Header block – PHPCodez\Layout\Block\Header.php with the following content.

<?php
 namespace PHPCodez\Layout\Block;
 class Header extends \Magento\Framework\View\Element\Template {
 
 protected $_headerContentFactory;
 
 public function __construct(\Magento\Framework\View\Element\Template\Context $context,
 \PHPCodez\Layout\Model\HeaderContentFactory $headerContentFactory) {
 $this->_headerContentFactory = $headerContentFactory;
 parent::__construct($context);
 }

public function getHeaderContent() {
 $headerContent = $this->_headerContentFactory->create();
 return $headerContent->process();
 }
 }

Here the block is calling the model function process().