Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Unfortunately nested classes are not a language feature of PHP.

The below SO page explains this in detail.

Nested or Inner Class in PHP

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top