PHP's get_defined_functions in TypeScript
✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { get_defined_functions } from 'locutus/php/funchand/get_defined_functions'.
Or with CommonJS: const { get_defined_functions } = require('locutus/php/funchand/get_defined_functions')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite ,
webpack ,
Rollup , and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
# code expected result
1 var $funcs = get_defined_functions()
var $result = Array.isArray($funcs) && $funcs.length > 0true
Notes
Dependencies
This function uses the following Locutus functions:
Here's what our current TypeScript equivalent to PHP's get_defined_functions looks like.
Copy code
import { ensurePhpRuntimeState, getPhpGlobalScope } from '../_helpers/_phpRuntimeState.ts' import { toPhpArrayObject } from '../_helpers/_phpTypes.ts' export function get_defined_functions ( ): string [] { ensurePhpRuntimeState () const arr : string [] = [] const already : Record <string , 1 > = {} const globalScope = getPhpGlobalScope () for (const i in globalScope) { try { const topLevelValue = globalScope[i] if (typeof topLevelValue === 'function' ) { if (!already[i]) { already[i] = 1 arr.push (i) } } else if (typeof topLevelValue === 'object' && topLevelValue !== null ) { const nestedObject = toPhpArrayObject (topLevelValue) for (const j in nestedObject) { if (typeof nestedObject[j] === 'function' && !already[j]) { already[j] = 1 arr.push (j) } } } } catch (_e) { } } return arr }
import { ensurePhpRuntimeState, getPhpGlobalScope } from '../_helpers/_phpRuntimeState.ts' import { toPhpArrayObject } from '../_helpers/_phpTypes.ts' export function get_defined_functions ( ) { ensurePhpRuntimeState () const arr = [] const already = {} const globalScope = getPhpGlobalScope () for (const i in globalScope) { try { const topLevelValue = globalScope[i] if (typeof topLevelValue === 'function' ) { if (!already[i]) { already[i] = 1 arr.push (i) } } else if (typeof topLevelValue === 'object' && topLevelValue !== null ) { const nestedObject = toPhpArrayObject (topLevelValue) for (const j in nestedObject) { if (typeof nestedObject[j] === 'function' && !already[j]) { already[j] = 1 arr.push (j) } } } } catch (_e) { } } return arr }
type PhpNullish = null | undefined type PhpInput = {} | PhpNullish type PhpList <T = PhpInput > = T[]type PhpAssoc <T = PhpInput > = { [key : string ]: T }type PhpArrayLike <T = PhpInput > = PhpList <T> | PhpAssoc <T>function isPhpList<T = PhpInput >(value : PhpInput ): value is PhpList <T> { return Array .isArray (value) } function isObjectLike (value : PhpInput ): value is PhpArrayLike <PhpInput > { return typeof value === 'object' && value !== null } function isPhpAssocObject<T = PhpInput >(value : PhpInput ): value is PhpAssoc <T> { return isObjectLike (value) && !isPhpList (value) } function isPhpArrayObject<T = PhpInput >(value : PhpInput ): value is PhpAssoc <T> { return isObjectLike (value) } function toPhpArrayObject<T = PhpInput >(value : PhpInput ): PhpAssoc <T> { if (isPhpArrayObject<T>(value)) { return value } return {} } interface IniEntry { local_value ?: PhpInput } type LocaleEntry = PhpAssoc <PhpInput > & { sorting ?: (left : PhpInput , right : PhpInput ) => number } type LocaleCategoryMap = PhpAssoc <string | undefined >interface LocutusRuntimeContainer { php ?: PhpAssoc <PhpInput > } type GlobalWithLocutus = { $locutus?: LocutusRuntimeContainer [key : string ]: PhpInput } interface PhpRuntimeState { ini : PhpAssoc <IniEntry | undefined > locales : PhpAssoc <LocaleEntry | undefined > localeCategories : LocaleCategoryMap pointers : PhpList <PhpInput > locale_default : string | undefined } const isIniBag = (value : PhpInput ): value is PhpAssoc <IniEntry | undefined > => isPhpAssocObject<IniEntry | undefined >(value) const isLocaleBag = (value : PhpInput ): value is PhpAssoc <LocaleEntry | undefined > => isPhpAssocObject<LocaleEntry | undefined >(value) const isLocaleCategoryBag = (value : PhpInput ): value is LocaleCategoryMap => isPhpAssocObject<string | undefined >(value)const globalContext : GlobalWithLocutus = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {} const ensurePhpRuntimeObject = (): PhpAssoc <PhpInput > => { let locutus = globalContext.$locutus if (typeof locutus !== 'object' || locutus === null ) { locutus = {} globalContext.$locutus = locutus } let php = locutus.php if (typeof php !== 'object' || php === null ) { php = {} locutus.php = php } return php } function ensurePhpRuntimeState ( ): PhpRuntimeState { const php = ensurePhpRuntimeObject () const iniValue = php.ini const localesValue = php.locales const localeCategoriesValue = php.localeCategories const pointersValue = php.pointers const ini = isIniBag (iniValue) ? iniValue : {} const locales = isLocaleBag (localesValue) ? localesValue : {} const localeCategories = isLocaleCategoryBag (localeCategoriesValue) ? localeCategoriesValue : {} const pointers : PhpList <PhpInput > = Array .isArray (pointersValue) ? pointersValue : [] if (iniValue !== ini) { php.ini = ini } if (localesValue !== locales) { php.locales = locales } if (localeCategoriesValue !== localeCategories) { php.localeCategories = localeCategories } if (pointersValue !== pointers) { php.pointers = pointers } const localeDefaultValue = php.locale_default const localeDefault = typeof localeDefaultValue === 'string' ? localeDefaultValue : undefined return { ini, locales, localeCategories, pointers, locale_default : localeDefault, } } function getPhpGlobalScope ( ): PhpAssoc <PhpInput > { return globalContext } function get_defined_functions ( ): string [] { ensurePhpRuntimeState () const arr : string [] = [] const already : Record <string , 1 > = {} const globalScope = getPhpGlobalScope () for (const i in globalScope) { try { const topLevelValue = globalScope[i] if (typeof topLevelValue === 'function' ) { if (!already[i]) { already[i] = 1 arr.push (i) } } else if (typeof topLevelValue === 'object' && topLevelValue !== null ) { const nestedObject = toPhpArrayObject (topLevelValue) for (const j in nestedObject) { if (typeof nestedObject[j] === 'function' && !already[j]) { already[j] = 1 arr.push (j) } } } } catch (_e) { } } return arr }
function isObjectLike (value ) { return typeof value === 'object' && value !== null } function isPhpAssocObject (value ) { return isObjectLike (value) && !Array .isArray (value) } function toPhpArrayObject (value ) { if (isPhpArrayObject (value)) { return value } return {} } const isPhpArrayObject = isObjectLikeconst globalContext = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {} const ensurePhpRuntimeObject = ( ) => { let locutus = globalContext.$locutus if (typeof locutus !== 'object' || locutus === null ) { locutus = {} globalContext.$locutus = locutus } let php = locutus.php if (typeof php !== 'object' || php === null ) { php = {} locutus.php = php } return php } function ensurePhpRuntimeState ( ) { const php = ensurePhpRuntimeObject () const iniValue = php.ini const localesValue = php.locales const localeCategoriesValue = php.localeCategories const pointersValue = php.pointers const ini = isPhpAssocObject (iniValue) ? iniValue : {} const locales = isPhpAssocObject (localesValue) ? localesValue : {} const localeCategories = isPhpAssocObject (localeCategoriesValue) ? localeCategoriesValue : {} const pointers = Array .isArray (pointersValue) ? pointersValue : [] if (iniValue !== ini) { php.ini = ini } if (localesValue !== locales) { php.locales = locales } if (localeCategoriesValue !== localeCategories) { php.localeCategories = localeCategories } if (pointersValue !== pointers) { php.pointers = pointers } const localeDefaultValue = php.locale_default const localeDefault = typeof localeDefaultValue === 'string' ? localeDefaultValue : undefined return { ini, locales, localeCategories, pointers, locale_default : localeDefault, } } function getPhpGlobalScope ( ) { return globalContext } function get_defined_functions ( ) { ensurePhpRuntimeState () const arr = [] const already = {} const globalScope = getPhpGlobalScope () for (const i in globalScope) { try { const topLevelValue = globalScope[i] if (typeof topLevelValue === 'function' ) { if (!already[i]) { already[i] = 1 arr.push (i) } } else if (typeof topLevelValue === 'object' && topLevelValue !== null ) { const nestedObject = toPhpArrayObject (topLevelValue) for (const j in nestedObject) { if (typeof nestedObject[j] === 'function' && !already[j]) { already[j] = 1 arr.push (j) } } } } catch (_e) { } } return arr }
Improve this function
Locutus is a community effort following
The McDonald's Theory :
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
View on GitHub
·
Edit on GitHub
·
View Raw
« More PHP funchand functions
Star