Working with namespaces

43 views
Skip to first unread message

Silent2049

unread,
May 20, 2020, 4:55:21 PM5/20/20
to Fat-Free Framework
Hello,

I've been working on developing my PHP skills and I really want to use F3 on my idea, but I'm having issues with autoloading and using namespaces. I feel like there is a disconnect somewhere for me and I just can't get passed it.

I'm referencing the directory structure here: https://groups.google.com/d/msg/f3-framework/8ELCyGC1HKA/VTpNZYicAwAJ

So I have the following folder layout:

-- /
---- /App
----- /Controller
------- /BaseSite.php
----- /Model
------- /User.php

In my controllers I use namespace Controller. In my models, I use Model.

Example code App/Controller/BaseSite.php:
<?php
namespace Controller;

Class BaseSite
{
...
public function somefunction($f3)
{
$user = new \Model\UserModel($f3);
}


Which results in: Class 'Model\UserModel' not found [D:\laragon\www\ntw\App\Controller\BaseSite.php:27]

In Visual Code, everything checks out in the namespaces and nothing has an error with the defined namespaces. I don't know what I'm doing wrong. Any pointers is greatly appreciated

Paul Herring

unread,
May 20, 2020, 5:07:48 PM5/20/20
to Silent2049 via Fat-Free Framework, Fat-Free Framework
>  $user = new \Model\UserModel($f3);

Shouldn't that be

> $user = new \Model\User($f3);

?

--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/0acd8bb0-3bcc-428d-a35b-8bc966d4747c%40googlegroups.com.


--
PJH

Jason Verges

unread,
May 20, 2020, 5:14:03 PM5/20/20
to Fat-Free Framework on behalf of PJH


I named the class inside the Model/User.php as User and UserModel as experimenting. It is currently Class UserModel inside User.php

On May 20, 2020, at 16:07, Fat-Free Framework on behalf of PJH <f3-fra...@googlegroups.com> wrote:



Paul Herring

unread,
May 20, 2020, 5:20:15 PM5/20/20
to Fat-Free Framework on behalf of Silent2049
On Wed, May 20, 2020 at 10:14 PM Fat-Free Framework on behalf of Silent2049 <f3-fra...@googlegroups.com> wrote:


I named the class inside the Model/User.php as User and UserModel as experimenting. It is currently Class UserModel inside User.php


I'd try and keep the naming all consistent. Either use the word model twice everywhere, or once everywhere.

I'd suggest the latter; \Model\UserModel is redundant.


richgoldmd

unread,
May 20, 2020, 5:26:28 PM5/20/20
to Fat-Free Framework
Best Practices for PHP suggest that there be one class per file and the class and file share the same name.



Jason Verges

unread,
May 20, 2020, 5:29:44 PM5/20/20
to richgoldmd via Fat-Free Framework
Wow. I changed the class name back to User and it works just fine now. I originally called it User and kept getting the error. Then I changed it to UserModel just for sanity checking. 

Everything works as expected now. 🥳

I feel silly, but thanks for the pointers everyone! 😅


On May 20, 2020, at 16:26, richgoldmd via Fat-Free Framework <f3-framework+APn2wQeVDXIhcWXnDF5...@googlegroups.com> wrote:


Best Practices for PHP suggest that there be one class per file and the class and file share the same name.



--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages