/    Sign up×
Community /Pin to ProfileBookmark

Loading Classs Error

I was going to load all my classes via a php script to save adding new classes as I create them. Using the code below.

The issue is I have a class apples.php for example, which is loaded before database.php due to the alphabetical order pages are found in.

apples.php is extended database.php so therefore I get this error

Fatal error: Uncaught Error: Class ‘dbh’ not found in

Other than renaming database.php a_database.php or something similar what else can I do?

Surely this is a common issue?

“`
foreach (glob(“../classes/*.php”) as $filename){
require_once $filename;
}
“`

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 23.2020 — Usually what I do is use require_once() at the top of each class's file for anything it depends on. I suppose a bit more verbose than trying to have just one call for each class, but at least the _once aspect means PHP only actually does the loading once, even if it gets parsed multiple times.
<i>
</i>&lt;?php
require_once './some_class.php;

class Foo extends SomeClass {
}

But...if you have a lot of classes and dependencies, you might want to look into setting up auto-loaders. https://www.php.net/manual/en/language.oop5.autoload.php is the tip of the iceberg, but if it sounds like it might be right for you, I'll let you search for better tutorials, videos, whatever. If you're careful/consistent with class and file naming, you just need some main start-up file that defines your autoload function(s), and the loading just happens.
Copy linkTweet thisAlerts:
@kiwisauthorOct 23.2020 — @NogDog#1624526

I had considered that, but how to get around it when Foo is called at various folder levels?

some_class.php won't always be back one directory.
Copy linkTweet thisAlerts:
@NogDogOct 23.2020 — @kiwis80#1624528

It will depend on how your app is constructed, I guess. The PHP app I work on at my job has every endpoint come in through a man "controller" file, and that file defines a constant like APP_ROOT. Then whenever I need to require something, it's like:
<i>
</i>require_once APP_ROOT.'/models/foo.php';

Alternatively, again depending on your config, you might be able to user $_SERVER['DOCUMENT_ROOT'] as the base point from which to reference a file.
Copy linkTweet thisAlerts:
@NogDogOct 23.2020 — PS: In case I'm over-complicating things, you can always reference relative to the calling file's directory with:
<i>
</i>require_once __DIR__.'../../some/dir/file.php';
Copy linkTweet thisAlerts:
@NachfolgerOct 24.2020 — > @kiwis80#1624528 I had considered that, but how to get around it when Foo is called at various folder levels?
>
> some_class.php won't always be back one directory.


Simple.
``PHP<i>
</i>require(dirname(__FILE__, 3) ."/assets/php/db.php");<i>
</i>
`</CODE>
Where <C>
3</C> indicates the level below the root (www directory) the given file is.

Eg. <C>
html/auth/user/file.php</C> aka <C>html/one/two/three.php</C>

With /<C>
assets</C> being contained in /<C>html</C>/<C>assets`
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...