Jump to content

Recommended Posts

Hello

 

If we pass a value to a object when creating it with __construct

$NewObject = new class($value)

Lets say our object is placed within a loop to do some further stuff

while(1 < 10)
{

$NewObject->doThis();
$NewObject->doThat();

1++;

}

Now, we want to pass the next value to the object, so it will doThis() and doThat in the loop, But currently, it will just perform doThis() and doThat() on the same value.

How is this professionally achieved?

We can destroy the object and put $NewObject = new class($value); inside the loop, But is there a better way?

 

Thanks in advance for any future help/thoughts.

Link to comment
https://forums.phpfreaks.com/topic/285165-passing-new-value-to-object-within-loop/
Share on other sites

I think I had a brain messup when designing this weeks ago.

I guess its "properly" done by passing the values in a method that's callable manually other that __construct.

 

This way we don't have to destroy and start new objects.

 

Sorry for the question, Unless I have still got it wrong, in which case the question still applies.

 

Thanks

You could store the value as a class property:

http://www.php.net/manual/en/language.oop5.properties.php

 

Yes that would work, with the setter of that object not being in a __construct.

 

With regards to this overall question, What about many properties that have been set later during the objects use?

 

example

 

var $date

var $time

 

Im guessing we should set all properties to null when we assign a new value to the "main" property?

otherwise, we will be using older set values of properties set from a previous "main" value, (within the same new instance however)

However if we just destroyed the object and started new, all of this would be done automatically, But I am wondering which is the professional way??

 

kill object, rebuild new

or

build once, reset all with new use(via method to set all var inside to null)

 

Thanks in advance.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.