Skip to main content
The ls command lists variables, constants, classes, interfaces, traits, functions, methods, and properties. Without arguments, it shows variables in the current scope. With a target, it shows members of that class or object.

Syntax

ls [options] [target]

Alias

  • dir

Arguments

target
code
A class name or object instance to list members of

Options

What to List

--vars
flag
Display variables in current scope (default when no target is given)
--constants
flag
Alias: -cDisplay defined constants
--functions
flag
Alias: -fDisplay defined functions
--classes
flag
Alias: -kDisplay declared classes
--interfaces
flag
Alias: -IDisplay declared interfaces
--traits
flag
Alias: -tDisplay declared traits
--properties
flag
Alias: -pDisplay class or object properties (public by default)
--methods
flag
Alias: -mDisplay class or object methods (public by default)

Filters

--grep
pattern
Filter results by pattern (supports regular expressions)
--insensitive
flag
Alias: -iMake grep search case-insensitive
--invert
flag
Alias: -vInvert the grep search (show non-matching items)
--globals
flag
Alias: -gInclude global variables
--internal
flag
Alias: -nLimit to internal (built-in) functions and classes
--user
flag
Alias: -uLimit to user-defined constants, functions, and classes
--category
string
Alias: -CLimit to constants in a specific category (e.g., “date”, “pcre”)

Display Options

--all
flag
Alias: -aInclude private and protected methods and properties
--long
flag
Alias: -lList in long format: includes class names and full signatures
--no-inherit
flag
Exclude inherited methods, properties, and constants

Usage Examples

List Variables

By default, ls shows variables in the current scope:
>>> $name = 'Alice'
>>> $age = 30
>>> ls
Variables: $name, $age, $_, $__psysh__

List Object Members

>>> $date = new DateTime()
>>> ls $date
Class Methods: __construct, __wakeup, __set_state, createFromFormat, getLastErrors,
               format, modify, add, sub, getTimezone, setTimezone, getOffset, setTime,
               setDate, setISODate, setTimestamp, getTimestamp, diff

List Class Members

>>> ls DateTime
>>> ls Psy\Shell
By default, shows constants, properties, and methods.

Long Format

Use -l for detailed signatures:
>>> ls -l DateTime
Class Methods:
  __construct          __construct($datetime = "now", $timezone = null)
  format               format($format)
  modify               modify($modifier)  
  add                  add($interval)
  sub                  sub($interval)
  diff                 diff($datetime2, $absolute = false)
  ...

Show Private/Protected Members

>>> ls -a MyClass
>>> ls --all $myObject
Includes private and protected methods and properties in addition to public ones.

Filter with Grep

>>> ls --methods DateTime --grep /format/
>>> ls -k --grep mongo -i
>>> ls --functions --grep /^array_/
>>> ls --functions --grep /^str_/ --long
Shows all functions starting with “str_” in long format:
Functions:
  str_replace    str_replace($search, $replace, $subject, &$count = null)
  str_split      str_split($string, $length = 1)
  str_pad        str_pad($string, $length, $pad_string = " ", $pad_type = STR_PAD_RIGHT)
  ...

List All Classes

>>> ls --classes
>>> ls -k
Shows all declared classes in the current session.

List Classes by Pattern

>>> ls -k --grep /DateTime/
>>> ls --classes --grep /^Psy/ -i

List All Functions

>>> ls --functions
>>> ls -f

List User-Defined Functions

>>> ls -f --user
>>> ls --functions -u
function myHelper($arg) { return strtoupper($arg); }

>>> ls -f --user
User Functions: myHelper

List Internal Functions

>>> ls -f --internal --grep /^array/
Shows built-in PHP functions matching the pattern.

List Constants

>>> ls --constants
>>> ls -c

List Constants by Category

>>> ls --constants --category date
>>> ls -c -C pcre
>>> ls -c -C date
Constants: DATE_ATOM, DATE_COOKIE, DATE_ISO8601, DATE_RFC822, DATE_RFC850,
           DATE_RFC1036, DATE_RFC1123, DATE_RFC2822, DATE_RFC3339, DATE_RSS,
           DATE_W3C, ...

Exclude Inherited Members

>>> ls --no-inherit MyChildClass
Shows only members defined directly in MyChildClass, not inherited from parents.

List Global Variables

>>> ls --vars --globals
>>> ls -g
Includes global variables like $_SERVER, $_GET, $_POST, etc.

Advanced Examples

Find All Array Functions

>>> ls -f --grep /^array_/ --long

List Private Methods

>>> ls -am MyClass
Shows all methods (-a includes private) in method-only view (-m).

Search for Specific Properties

>>> ls -p $object --grep /name/

List All Traits

>>> ls --traits
>>> ls -t

List Interfaces

>>> ls --interfaces --grep /Iterator/
>>> ls -I --grep /Iterator/

Default Behavior

Without Target

Defaults to --vars:
>>> ls
# Equivalent to: ls --vars

With Target

Defaults to --constants --properties --methods:
>>> ls DateTime  
# Equivalent to: ls --constants --properties --methods DateTime

Output Format

Short Format (Default)

Items are listed inline, separated by commas:
Variables: $foo, $bar, $baz
Methods: getName, setName, getAge, setAge

Long Format (-l)

Items are listed one per line with full signatures:
Methods:
  getName    getName()
  setName    setName($name)
  getAge     getAge()  
  setAge     setAge($age)

Visibility Indicators

When using -a (all), members are color-coded:
  • Public - normal color
  • Protected - different color
  • Private - different color

Magic Variables

After listing a class or object:
>>> ls Psy\Shell
>>> echo $__class
Psy\Shell
>>> echo $__namespace  
Psy

Tips

Combine flags for powerful filtering:
>>> ls -al DateTime --grep /set/
Shows all methods (including private) containing “set” in long format.
Use regular expressions in grep:
>>> ls -f --grep "/^(str|array)_/"
Shows functions starting with “str_” or “array_”.
Explore an unknown object:
>>> ls -al $unknownObject
Reveals all properties and methods, including private ones.
Options like --properties and --methods require a target:
>>> ls --properties        # Error!
>>> ls --properties $obj   # OK

See Also

  • doc - Read documentation for listed items
  • show - View source code
  • dump - Inspect object values

Build docs developers (and LLMs) love