/    Sign up×
Community /Pin to ProfileBookmark

Remove Database in Object when returning an object

I have a class which which has all the usual properties of a person, it also has a database property as I pass in my database connection.

When I return a instance of my object I pass back a heap of database stuff too which I don’t want to do.

How can I stop this?

“`
class Person{

private $dbh;

private int $Id;
private string $Surname;
private string $Firstname;
private string $Middlenames;
private string $knownBy;
private string $gender;

public function __construct(database $database){
$this->dbh = $database;
}

public function getById($Id){

“`

If I do

“`
$Person = new Person($database);
echo $Person(23);
“`

I get things like

“`
array(7874) {
[0]=>
object(Person)#4 (16) {
[“dbh”:”Person”:private]=>
object(database)#1 (18) {
[“affected_rows”]=>
int(-1)
[“client_info”]=>
string(14) “mysqlnd 7.4.33”
[“client_version”]=>
int(70433)
[“connect_errno”]=>
int(0)
[“connect_error”]=>
NULL
“`

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 29.2022 — I'm a bit confused: do you actually do echo $Person(23);, or is it really echo $Person->getById(23);...or something else? The first looks like it should just not work, the second would be driven by what that method does, and as far as "something else"... 🤷
Copy linkTweet thisAlerts:
@kiwisauthorDec 29.2022 — Sorry I echo

echo $Person->getById(23)


but I don’t want the DB info
Copy linkTweet thisAlerts:
@NogDogDec 29.2022 — > @kiwis80#1649493 but I don’t want the DB info

Well, you could (a) edit that method to only return what you want, or (b) add a new method that does what you want. For now it's a black box to us what that method is currently doing (other than it looks like it uses something such as var_export() to create the text, maybe?)
×

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.25,
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,
)...