PHP Classes

Has Parent: Trait to assign on object as parent of another

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 70 All time: 10,288 This week: 39Up
Version License PHP version Categories
has-parent 0.0.2GNU General Publi...5.6PHP 5, Language, Traits
Description 

Author

This package provides a trait to assign on object as parent of another.

It provides a function that takes one object and assigns it as the parent of another object of another class.

When the same function is called without parameters it returns the previously assigned parent object, so the parent object functions and variables can be accessed.

Innovation Award
PHP Programming Innovation award nominee
September 2016
Number 13
PHP provides means to reuse classes that extend functionality of others, but that requires that classes be defined as sub-classes.

If you want to combine objects of different classes, PHP does not provide a built-in way for that.

This package provides a trait that can be used to emulate inheritance between objects of different classes.

Manuel Lemos
Picture of magog
  Performance   Level  
Name: magog <contact>
Classes: 3 packages by
Country: Russian Federation Russian Federation
Age: 47
All time rank: 286279 in Russian Federation Russian Federation
Week rank: 268 Up18 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 2x

Documentation

HasParent

Description

This trait assigns parent to a class.

License

HasParent is open-sourced software licensed under the GPL-3.0.

Requirements

Package depends on __deferred-exceptions__ package.

  1. GitHub: * seotils/deferred-exceptions
  2. Composer (packagist.org): * seotils/deferred-exceptions
  3. PHPClasses: * Deferred Exceptions

Install

composer require seotils/has-parent

Usage

<?php

namespace My;

use Seotils\Traits\HasParent;

/
 * Parent class
 */
class A {
  public function time(){
    return date('Y-m-d H:i:s');
  }
}

/
 * Class with HasParent trait
 */
class B {
  use HasParent;
}

// Usage

$a = new A();
$b = new B();

// Assign and use parent class
echo $b->parentClass( $a )->time();

// Or step by step

// Assign parent class
$b->parentClass( $a );

// Use parent class
echo $b->parentClass()->time();

That`s all!


  Files folder image Files (5)  
File Role Description
Files folder imagesrc (2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Class source

  Files folder image Files (5)  /  src  
File Role Description
Files folder imageInterfaces (1 file)
Files folder imageTraits (1 file)

  Files folder image Files (5)  /  src  /  Interfaces  
File Role Description
  Plain text file IHasParent.php Class Class source

  Files folder image Files (5)  /  src  /  Traits  
File Role Description
  Plain text file HasParent.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadhas-parent-2022-10-21.zip 14KB
Downloadhas-parent-2022-10-21.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Deferred Exceptions Download .zip .tar.gz It`s a part of the package Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:70
This week:0
All time:10,288
This week:39Up