1

I have a class called Pages used as a namespace, like this:

<?php

static class Pages
{
    class Page
    {
        public $Name;
        public $Title;

        public function __construct($Name, $Title)
        {
            $this->Name = $Name;
            $this->Title = $Title;
        }
    }
}

?>

Elsewhere:

<?php

$g_Pages = new Pages::Page("My Name", "My Title");

?>

Unfortunately, I'm getting a Parse error: syntax error, unexpected 'Page' (T_STRING), expecting variable (T_VARIABLE) or '$'

What am I doing wrong?

Lakey
  • 1,948
  • 2
  • 17
  • 28
  • 2
    The error message doesn't appear to relate to the code you have posted. –  Dec 16 '13 at 01:26
  • Yeah sorry, I was trying to anonymize the example. The original class names had a different name. I just edited it to fix. – Lakey Dec 16 '13 at 01:33

2 Answers2

2

Unfortunately nested classes are not a language feature of PHP.

The below SO page explains this in detail.

Nested or Inner Class in PHP

Community
  • 1
  • 1
robbmj
  • 16,085
  • 8
  • 38
  • 63
2

If you're trying to use the inner class as a namespace, just use namespaces. Ex:

<?php
namespace Pages;
class Page { }

Then you can access the class through:

$g_Pages = new \Pages\Page("My Name", "My Title");

http://php.net/namespaces

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95