/    Sign up×
Community /Pin to ProfileBookmark

OOP – User Class

I’m looking to have a user class, where I can register users, log them in and out etc.

My methods will be

logInUser()
RegisterUser()
ValidateEmail()
LogoutUser()
getUserFromCookie($oken, $id)

I’ll have a property such as isLoggedIn with a method of similar name

My question is on my page, I can call $User = new User(); 

From there I would do $User->getUserFromCookie($_COOKIE[‘userToken’], $_COOKIE[‘userId’]);

If the user token and Id matches I’ll get the user details from my db. Otherwise I don’t want to create a user object. 

How can I do a if statement? such as if($User->isLoggedIn() === true) { when I’ve not set that property?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
Dec 14.2024 — Nice article
Copy linkTweet thisAlerts:
Image@ccarteralex1997-altJan 15.2026 — Initialize isLoggedIn with a default value (e.g., false) in the constructor, then update it inside getUserFromCookie() only when validation succeeds. This ensures isLoggedIn() is always defined and lets you reliably check the user state without conditionally creating the object.
×

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 1.24,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
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: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @dert,
tipped: article
amount: 1000 SATS,

tipper: @viney352,
tipped: article
amount: 10 SATS,
)...