Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Adding a Basic Form Using A Third-Party Library

Fedor Zhorin
PLUS
Fedor Zhorin
Courses Plus Student 3,940 Points

How to fix the PHP namespace parse error?

I am trying to get more familiar with "namespaces" that are used in this lesson. So I just copy and paste the 1st example from php.net documentation into the workspace.

Example #1 Declaring a single namespace

<?php 

namespace MyProject;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }

?>

But when I try to run this script I get the PHP parse error:

PHP Parse error: syntax error, unexpected 'Myproject' (T_STRING) in /home/treehouse/workspace/lib1.php on line 3.

This example from php.net so I dont't think that the problem with the code. Please help me

Just tried this in workspaces and it seems to work ok. Are you able to see this workspace?

https://teamtreehouse.com/workspaces/38887782#

_Ben

Hey Fedor - I'll just put the files below here. At the top navigation, you just click workspaces and add a PHP workspace to test these out.

lib1.php

<?php

namespace MyProject;

class Connection {
  public function connect()
  {
    echo "Connected" . PHP_EOL;
  }
}

function connect()
{
  echo "Connected" . PHP_EOL;
}

script.php

<?php

require_once __DIR__ .'/lib1.php';

$lib = new MyProject\Connection;

$lib->connect();

MyProject\connect();

Also, I would move your answer to the comments section ;)

Cheers,

_Ben

1 Answer

Fedor Zhorin
PLUS
Fedor Zhorin
Courses Plus Student 3,940 Points

Hi, Ben! Thanks for your reply! I am trying to open your link to the workspace, but it seems that such page doesn't exist(( Maybe I have no access to this workspace while I don't start the apropriate course?! So, Ben, can you say with what course this workspace is related?