-1

Assume I have the following classes: Attributes, Quote given:

    class Attributes {
         private $attr1; 

         public function __construct($attr)
         {
              // implementation 
         }

   }

   class Quote {
       private $obj;

       public function __construct($id, $obj)
       {
           $this->obj = $obj;  
           var_dump($this->obj); 
       }
   }

Can I therefore, somehow, pass in the object to the constructor for Quote, like this:

$attr = new Attributes("1");
$quote = new Quote(1, $attr);

Doing this, just gives me a blank page?

Phorce
  • 4,424
  • 13
  • 57
  • 107

2 Answers2

1

It doesn't work because you're trying to access the variables directly. You can either make the variables public or you can write get functions in your Attributes class.

Also you made a typing mistake in your full example ;)

Raiment
  • 91
  • 3
0

See PHP: Visibility :http://php.net/manual/en/language.oop5.visibility.php for details on the difference between private, protected and public.

Here's your code :

<?php
    session_start();

    class Attributes {

        public $intro;
        protected $column1;
        protected $column2;
        protected $cost;
        protected $dateValid;
        protected $TAC;

        public function __construct($theIntro, $theColumn, $theColumn2, $theCost, $theDateValid, $theTAC)
        {
            $this->intro   = $theIntro;
            $this->column1 = $theColumn;
            $this->column2 = $theColumn2;
            $this->cost    = $theCost;
            $this->dateValid = $theDateValid;
            $this->TAC = $theTAC;
        }
    }

    class Quote {

        private $QuoteID;
        private $attr;

        public function __construct($id, $obj)
        {
            $this->attr = $obj;
            $this->QuoteID = $id;

            echo $this->attr->intro; 

            var_dump($this->attr->intro);
        }



    }


$attr = new Attributes("1", "2", "3", "4", "5", "6");
$quote = new Quote("1", $attr); 
?>
Hardy Mathew
  • 684
  • 1
  • 6
  • 22