1

I am trying to write the following code, though it is wrong it will probably demonstrate best why I am trying to do.

class myClass
{
    private $name = "";
    private $startAddress = new myAddress();  // this is the issue
    private $endAddress = new myAddress();    // this is the issue
}

How can I accomplish this properly?

Thank You

1 Answer 1

5
 <?php
class myClass {
   private $endAddress;
   public function __construct() {
       $this->endAddress = new myAddress();
   }
 }

Take advantage of the constructor, which is called every time you create a new object.

Sign up to request clarification or add additional context in comments.

3 Comments

@alex: If you omit the visibility, public is assumed.
@KingCrunch I like to show it. Otherwise it feels like I'm still using PHP4 :P
Thank you all very much! So speedy with the response! This was a big help. Thanks again! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.