C
C
chelkaz2016-10-01 07:06:30
PHP
chelkaz, 2016-10-01 07:06:30

Writes Class not found. How to correctly define the namespace?

Created a class from the site root:
/bitrix/templates/app/Pi/Test.php
Class:

namespace app\Pi;
class test extends CBitrixComponent
{
// ...
}

I try to connect in another file and make it work:
$test = new app\Pi\test;
// ...

But it says Class 'app\Pi\test' not found

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Nikolaev, 2016-10-01
@gromdron

Uh... what do you want to do?
Here's what I'm talking about: when using OOP in bitrix, you immediately face 2 big walls - the use of OOP and Bitrix. Before disassembling a specific code, I would like to enlighten you a little in the Bitrix architecture itself.
In Bitrix terminology, he uses the MVC concept, but within the framework of Bitrix, which means that on any page where you communicate with him, you must connect a prologue (at least prolog_before) and an epilogue (at least epilog_after, well, ideally). It is the "component" that is the MVC (In general, it is rather VC + row access, since there is essentially no model). There is a CBitrixComponent class (controller) and a CBitrixComponentTemplate class (which, together with the template files, forms a view).
The component is called from the $APPLICATION global variable via the IncludeComponent method. Before version 14 (if my memory serves me) there was no such thing as class.php at all, where you could inherit from CBitrixComponent and override some functions (for example, change the template engine), and after the new d7 core, it became possible to inherit from CBitrixComponent and place your code in the class.php file (this way we save both class.php with new functions and component.php with logic), and by overriding the executeComponent method you can get rid of component.php as well.
From the point of view of architecture (I recommend studying the documentation, it is very extensive and informative), all components should lie in the so-called namespace, which is usually used by a nickname or company name (Naturally, it does not start with a number and contains only Latin letters), which in turn the queue can be located in /bitix/components/#namespace#/#component_name#/
Now let's analyze your question line by line:
You put your class here: /bitrix/templates/app/Pi/Test.php, which is incorrect from an architectural point of view, since you placed the class responsible for the controller in the template folder. If you do not write your own module, you can include it, for example, in /local/php_interface/classes/ (the example is far-fetched, you can simply put it in /local/php_interface or immediately in init.php), but for this you will need to write an autoloader, since for Bitrix, it only works for modules. Suppose you write a rough require_once in init.php with a file.
Then you do the following:
Which is wrong, since you are essentially declaring a namespace for the file, and from the php point of view it looks like this in the end:
class app\Pi\test extends app\Pi\CBitrixComponent
{
// ...
}
And the app\Pi\CBitrixComponent class does not exist. So you need to correct the example at least like this:
class test extends \CBitrixComponent I
strongly recommend reading the documentation or at least watching the video from the Bitrix Academy. The system itself is not a gift, but another programmer who sees your code will say that Bitrix is ​​to blame.

M
Maxim, 2016-10-01
@khrisanfov

I am not familiar with Bitrix, but I suspect that the class name should be Test and not test

A
Alexander Filippenko, 2016-10-01
@alexfilus

By itself, the fact that the file with the class is in the folder with the template will not become visible. You need to either include it, or use autoloading classes php.net/manual/ru/language.oop5.autoload.php
Or use Bitrix tools. Usually classes are in modules, but since you inherit from CBitrixComponent, I will assume that the component is suitable for your purposes.
https://dev.1c-bitrix.ru/learning/course/?COURSE_I...

N
novrm, 2016-10-01
@novrm

It was like this:
Try this:
But in general, Everything should be done from the Capital. Like this:
$test = new \App\Pi\Test();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question