Class: PHP::Variable

Inherits:
PHP::Identifier show all
Defined in:
lib/php/syntax/variable.rb

Overview

See Also:

Constant Summary

Constants inherited from PHP::Identifier

SYNTAX

Instance Attribute Summary

Instance Method Summary

Methods inherited from PHP::Identifier

#<=>, #to_sym, valid_name?

Methods inherited from PHP::Node

#<<, #<=>, #children?, #each, #indent, #indent=, #to_s

Constructor Details

- (Variable) initialize(name, options = {})

A new instance of Variable

Parameters:

  • (Symbol, #to_s) name
  • (Hash{Symbol => Object}) options (defaults to: {})

Options Hash (options):

  • (Boolean) :global — default: false


13
14
15
16
17
18
19
20
21
22
# File 'lib/php/syntax/variable.rb', line 13

def initialize(name, options = {})
  @name = case name
    when Variable
      name.to_php
    else
      raise ArgumentError.new("invalid PHP variable name: #{name.inspect}") unless self.class.valid_name?(name)
      name.to_s.to_sym
  end
  @options = options
end

Instance Attribute Details

- (Symbol) name

Returns:

  • (Symbol)


7
8
9
# File 'lib/php/syntax/variable.rb', line 7

def name
  @name
end

Instance Method Details

- (Boolean) global?

Returns true if this is a global variable.

Returns:

  • (Boolean)


28
29
30
# File 'lib/php/syntax/variable.rb', line 28

def global?
  @options[:global] == true
end

- (Boolean) local?

Returns true if this is a local variable.

Returns:

  • (Boolean)


36
37
38
# File 'lib/php/syntax/variable.rb', line 36

def local?
  !global?
end

- (String) to_php

Returns the PHP representation of this variable.

Returns:

  • (String)


44
45
46
# File 'lib/php/syntax/variable.rb', line 44

def to_php
  global? ? "$GLOBALS['#{name}']" : "$#{name}"
end