Allow a specific class to access a private variable of another class

Allow a specific class to access a private variable of another class. But I am not willing to make that variable public.

Look into getters and setters

But we can modify that variable using setters which does not solve my motive.

You haven’t really explained what you’re trying to achieve so it’s a bit hard to recommend something.

How does a getter not solve the problem?

I have a Model class with a variable $isRetrievedFromDB (so that when I call save (from a DBAction object), it can decide whether it has to do a INSERT or UPDATE. Now, when I retrieve it from the database with the retrieve function, it will set the variable to true. Now, I do not want that the variable is open for tweaking, messing up the purpose. Please let me know what other info you need in case.

A simple code example would help. It’s not clear why this is a problem. You don’t want to make the variable public so a use case showing where you WANT some external code to update this variable would help.

Assuming you let the DB create the ID/PK then I want to propose a different solution.

$user = new UserModel;
$user->setUsername = 'JimL'
     ->setPassword = '3025ju283t';
$db->save($user);

Now just use $user->getId() to decide whether or not you have an entity that is stored in the DB or not. Ie have it return null by default, and obviously the actual ID in case the data is loaded from the DB.

That means one extra query. So will it effect the performance? If so, is there an other way around and I can change my whole idea. If not, how many queries are acceptable for just a single thing.

What means one extra query? From the code I pasted I’d only assume one query would ever run (saving the user).

I wrote about the query for retrieving the user plus the check query.

I’m not sure I understand.

What I was thinking…

creating a new user:

$user = new UserModel;
$user->setUsername = 'JimL'
     ->setPassword = '3025ju283t';
$db->save($user);

When saving you can get the id of the entity you wish to save with $user->getId(), in this case it will return null since we never set an ID. You can then assume it’s a new user that should be inserted.


VS ie updating the username

$user = // retrieve user from the db, however you do it
$user->setUsername = 'JimL';
$db->save($user);

When saving you can get the id of the entity you wish to save with $user->getId(), in this case it will return the ID of the user since we got the user from the database. You can then assume it’s an existing user that should be updated.


No need for an extra query ^^

You understood it absolutely correct and now I too. Actually, it did not strike me that there will no need of an ID when we are creating the model using new. So, we do not need another query. Now I get it. Thanks for helping me out. Appreciate your time. This site is better than SO for newbies and people who are a little weak in English. Again, thanks JimL.

Sponsor our Newsletter | Privacy Policy | Terms of Service