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

Fedor Zhorin
PRO
Fedor Zhorin
Pro 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
PRO
Fedor Zhorin
Pro 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?