PHP Classes

Get PHP Class Name: Get the base class name and namespace of an object

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 173 This week: 1All time: 8,797 This week: 560Up
Version License PHP version Categories
classname 1.2.1MIT/X Consortium ...5PHP 5, Language
Description 

Author

This package can get the base class name and namespace of an object.

It can take as parameter a string as class name or an object to get its class.

The class splits the class name in its components and can return the full class name, the namespace, the canonical name and the short class name.

Innovation Award
PHP Programming Innovation award nominee
August 2018
Number 4
PHP provides a function named get_class that can return the class of an object.

This class extends the capabilities of the get_class function by parsing the get_class return value and splitting the class name in its components like the full class name, the namespace, the canonical name and the short class name.

Manuel Lemos
Picture of Laudir Bispo
  Performance   Level  
Name: Laudir Bispo <contact>
Classes: 7 packages by
Country: Brazil Brazil
Age: 31
All time rank: 2248152 in Brazil Brazil
Week rank: 106 Up7 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

Example

Get class names

This package can get the name of the base class, the namespace, the canonical name, the parent class name, and the path of an object. It can have as a parameter a string as a class name or an object to get its class.

This class was nominated for the Innovation award from phpclasses.org

LooK Here - https://www.phpclasses.org/package/10819-PHP-Get-the-base-class-name-and-namespace-of-an-object.html

Installation

Install the latest version with

$ composer require laudirbispo/classname

Basic Usage

<?php

use laudirbispo\classname\ClassName;

$exampleClass = 'namespace\namspace2\MyClass';
// or
$exampleClass = new MyClass;

var_dump(ClassName::full($exampleClass));
// return string 'namespace\namspace2\MyClass' (length=27)

var_dump(ClassName::namespace($exampleClass));
// return string 'namespace\namspace2' (length=19)

var_dump(ClassName::short($exampleClass));
//return string 'MyClass' (length=7)

var_dump(ClassName::canonical($exampleClass));
// return string 'namespace.namspace2.MyClass' (length=27)

// Get parent class name or null
// @param $return string - full, namespace, canonical short - default is dull
var_dump(ClassName::getParent($exampleClass, string $return = 'full'));


Author

Laudir Bispo - <laudirbispo@outlook.com> - <https://twitter.com/laudir_bispo><br />

License

ClassName is licensed under the MIT License - see the LICENSE file for details Free Software, Hell Yeah!


  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Example Example script

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

 Version Control Reuses Unique User Downloads Download Rankings  
 100%2
Total:173
This week:1
All time:8,797
This week:560Up