PHP Classes

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-21 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 68 This week: 1All time: 10,082 This week: 491Up
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
Innovation award
Innovation award
Nominee: 2x

Details

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  
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  /  src  
File Role Description
Files folder imageInterfaces (1 file)
Files folder imageTraits (1 file)

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

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

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:68
This week:1
All time:10,082
This week:491Up