J
J
JackShcherbakov2018-01-28 21:44:47
PHP
JackShcherbakov, 2018-01-28 21:44:47

Why is there a php namespace issue?

Hello! I am new to PHP (4 days programming on it). Got to the namespaces.
There are 3 files:
1st - code1.php -

<?php namespace CodeOne;
function sayHi(){
  print "Передаю привет из code1";
}
?>

2nd - code2.php -
<?php namespace CodeTwo;
function sayHi(){
  print "Передаю привет из code2";
}
?>

3rd - general.php -
<?php 
require("code1.php");
require("code2.php");
print "файл general.php успешно подключен";
\CodeTwo\sayHi();
?>

This is what the file folder looks like:
  1. code1.php
  2. code2.php
  3. general.php

When executing general.php, the following error occurs:
Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in C:\OpenServer\domains\asd\code1.php on line 3

What is the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolay, 2018-01-28
@JackShcherbakov

  • The namespace declaration must not be preceded by PHP code, including extra spaces.
    See more: Defining Namespaces

D
Dmitry, 2018-01-28
@slo_nik

Good evening.
See the link for useful articles about namespaces.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question