title: Value not available within a private class
body: <p>My php script has two sql statements. The second one (connection 2) is not executing. I believe its because the value for 'id' is not set since it's within a private class. I was wondering if anyone had a suggestion on how to fix this?</p>
  
  <pre><code>        &lt;?php
      //process pdf file upload
          if (isset($_FILES["flyer"]["name"])); {
              $allowedExtsf = array("pdf");
              $tempf = explode(".", $_FILES["flyer"]["name"]);
              $extensionf = end($tempf);
              if (($_FILES["flyer"]["type"] == "application/pdf") &amp;&amp; ($_FILES["flyer"]["size"] &lt; 524288000) &amp;&amp; in_array($extensionf, $allowedExtsf)) 
              {
  
                        if (file_exists("../flyers/" . $_FILES["flyer"]["name"])) 
                             {
      //if file exists, delete the file on the server
                                unlink("../flyers/" . $_FILES["flyer"]["name"]);
                             }
      //move currrent pdf to the flyers folder
              move_uploaded_file($_FILES["flyer"]["tmp_name"],"../flyers/" . $_FILES["flyer"]["name"]);
      //Make url of pdf file                  
              $ad_link="http://www.website.com/flyers/" . $_FILES["flyer"]["name"];
  
      //SQL statement 1, insert all form fields, file url and current date time
            }
            else {
                $ad_link = NULL;
            }
        require('../dbcon2.php');
      //Connection 1
          $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
          $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $stmt = $conn-&gt;prepare("INSERT INTO listings (title, address, lot_size, zoning, build_size, sale_price, lease_price, comment, transaction, ad_link, date_added) VALUES (:title, :address, :lot_size, :zoning, :build_size, :sale_price, :lease_price, :comment, :transaction, :ad_link, now())");
      //Bind
            $stmt-&gt;bindParam(':title', $_POST['title']); 
            $stmt-&gt;bindParam(':address', $_POST['address']);
            $stmt-&gt;bindParam(':lot_size', $_POST['lot_size']);
            $stmt-&gt;bindParam(':zoning', $_POST['zoning']);
            $stmt-&gt;bindParam(':build_size', $_POST['build_size']);
            $stmt-&gt;bindParam(':sale_price', $_POST['sale_price']);
            $stmt-&gt;bindParam(':lease_price', $_POST['lease_price']);
            $stmt-&gt;bindParam(':comment', $_POST['comment']);
            $stmt-&gt;bindParam(':transaction', $_POST['transaction']);
            $stmt-&gt;bindParam(':ad_link', $ad_link);
          $stmt-&gt;execute();
          $id = $conn-&gt;lastInsertId();
          $title = $_POST['title'];
          $address = $_POST['address'];
          $lot_size = $_POST['lot_size'];
          $zoning = $_POST['zoning'];
          $build_size = $_POST['build_size'];
          $sale_price = $_POST['sale_price'];
          $lease_price = $_POST['lease_price'];
          $comment = $_POST['comment'];
          $transaction = $_POST['transaction'];
          $conn = null;
          }
      //Create class
        class CropAvatar {
            private $src;
            private $id;
            private $title;
            private $address;
            private $lot_size;
            private $zoning;
            private $build_size;
            private $sale_price;
            private $lease_price;
            private $comment;
            private $transaction;
            private $data;
            private $file;
            private $dst;
            private $type;
            private $extension;
      //location to save original image
            private $srcDir = '../0images/listimg/orig';
      //location to save cropped image
            private $dstDir = '../0images/listimg/mod';
            private $msg;
      //Add to consttruct
        function __construct($src, $data, $file, $id, $title, $address, $lot_size, $zoning, $build_size, $sale_price, $lease_price, $comment, $transaction) {
            $this -&gt; setSrc($src);
            $this -&gt; setData($data);
            $this -&gt; setFile($file);
            $this -&gt; setId($id);
            $this -&gt; setTitle($title);
            $this -&gt; setAddress($address);
            $this -&gt; setLot_size($lot_size);
            $this -&gt; setZoning($zoning);
            $this -&gt; setBuild_size($build_size);
            $this -&gt; setSale_price($sale_price);
            $this -&gt; setLease_price($lease_price);
            $this -&gt; setComment($comment);
            $this -&gt; setTransaction($transaction);  
            $this -&gt; crop($this -&gt; src, $this -&gt; dst, $this -&gt; data, $this -&gt; lastid, $this -&gt; title, $this -&gt; address, $this -&gt; lot_size, $this -&gt; zoning, $this -&gt; build_size, $this -&gt; sale_price, $this -&gt; lease_price, $this -&gt; comment, $this -&gt; transaction);
        }
                public function setId($id) {
                         $this-&gt;id = $id;
        }
                public function setTitle($title) {
                         $this-&gt;title = $title;
        }
                public function setAddress($address) {
                         $this-&gt;address = $address;
        }
                public function setLot_size($lot_size) {
                         $this-&gt;lot_size = $lot_size;
        }
                public function setZoning($zoning) {
                         $this-&gt;zoning = $zoning;
        }
                public function setBuild_size($build_size) {
                         $this-&gt;build_size = $build_size;
        }
                public function setSale_price($sale_price) {
                         $this-&gt;sale_price = $sale_price;
        }
                public function setLease_price($lease_price) {
                         $this-&gt;lease_price = $lease_price;
        }
                public function setComment($comment) {
                         $this-&gt;comment = $comment;
        }
                public function setTransaction($transaction) {
                         $this-&gt;transaction = $transaction;
        }
        //NNEED TO SET THE VARIABLES
  
                private function setSrc($src) 
                {
                    if (!empty($src)) 
                    {
                        $type = exif_imagetype($src);
                        if ($type) 
                        {
                            $this -&gt; src = $src;
                            $this -&gt; type = $type;
                            $this -&gt; extension = image_type_to_extension($type);
                            $this -&gt; setDst();
                        }
                    }
                }
                private function setData($data) 
                {
                    if (!empty($data)) 
                    {
                        $this -&gt; data = json_decode(stripslashes($data));
                    }
                }
                private function setFile($file) 
                {
                    $errorCode = $file['error'];
                    if ($errorCode === UPLOAD_ERR_OK) 
                    {
                        $type = exif_imagetype($file['tmp_name']);
                        if ($type) 
                        {
                            $dir = $this -&gt; srcDir;
                            if (!file_exists($dir)) 
                            {
                                mkdir($dir, 0777);
                            }
                            $currdate=date('YmdHis');
                            $extension = image_type_to_extension($type);
                            $src = $dir . '/' . $currdate . $extension;
                            if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG) {
                                if (file_exists($src)) 
                                {
                                    unlink($src);
                                }
                            $result = move_uploaded_file($file['tmp_name'], $src);
      //Connection 2 - Update sql row according to row id with the url of cropped image
                            $listing_img="http://www.website.com/0images/listimg/mod/" . $currdate . $extension;
                            $GLOBALS[ 'listing_img' ];
                        require('../dbcon2.php');
                            $GLOBALS[ 'id' ];
                              $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                              $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                              $sql="UPDATE listings SET listing_img='$listing_img' WHERE id=$this-&gt;id";
                              $conn-&gt;exec($sql);
                              $conn = null;
      //Error handling
                        if ($result) {
                            $this -&gt; src = $src;
                            $this -&gt; type = $type;
                            $this -&gt; extension = $extension;
                            $this -&gt; setDst();
                        } else {
                             $this -&gt; msg = 'Failed to save image file';
                        }
                    } else {
                        $this -&gt; msg = 'Please upload image with the following types only: JPG, PNG, GIF';
                    }
                } else {
                    $this -&gt; msg = 'Please upload image file';
                }
            } else {
                $this -&gt; msg = $this -&gt; codeToMessage($errorCode);
            }
        }
        private function setDst() {
            $dir = $this -&gt; dstDir;
            if (!file_exists($dir)) {
                mkdir($dir, 0777);
            }
            $this -&gt; dst = $dir . '/' . date('YmdHis') . $this -&gt; extension;
        }
        private function crop($src, $dst, $data) {
            if (!empty($src) &amp;&amp; !empty($dst) &amp;&amp; !empty($data)) {
                switch ($this -&gt; type) {
                    case IMAGETYPE_GIF:
                        $src_img = imagecreatefromgif($src);
                        break;
                    case IMAGETYPE_JPEG:
                        $src_img = imagecreatefromjpeg($src);
                        break;
                    case IMAGETYPE_PNG:
                        $src_img = imagecreatefrompng($src);
                        break;
                }
                if (!$src_img) {
                    $this -&gt; msg = "Failed to read the image file";
                    return;
                }
                $dst_img = imagecreatetruecolor(220, 220);
                $result = imagecopyresampled($dst_img, $src_img, 0, 0, $data -&gt; x, $data -&gt; y, 220, 220, $data -&gt; width, $data -&gt; height);
                if ($result) {
                    switch ($this -&gt; type) {
                        case IMAGETYPE_GIF:
                            $result = imagegif($dst_img, $dst);
                            break;
                        case IMAGETYPE_JPEG:
                            $result = imagejpeg($dst_img, $dst);
                            break;
                        case IMAGETYPE_PNG:
                            $result = imagepng($dst_img, $dst);
                            break;
                    }
                    if (!$result) {
                        $this -&gt; msg = "Failed to save the cropped image file";
                    }
                } else {
                    $this -&gt; msg = "Failed to crop the image file";
                }
                imagedestroy($src_img);
                imagedestroy($dst_img);
            }
        }
                private function codeToMessage($code) {
                    switch ($code) {
                        case UPLOAD_ERR_INI_SIZE:
                            $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                            break;
                        case UPLOAD_ERR_FORM_SIZE:
                            $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                            break;
                        case UPLOAD_ERR_PARTIAL:
                            $message = 'The uploaded file was only partially uploaded';
                            break;
                        case UPLOAD_ERR_NO_FILE:
                            $message = 'No file was uploaded';
                            break;
                        case UPLOAD_ERR_NO_TMP_DIR:
                            $message = 'Missing a temporary folder';
                            break;
                        case UPLOAD_ERR_CANT_WRITE:
                            $message = 'Failed to write file to disk';
                            break;
                        case UPLOAD_ERR_EXTENSION:
                            $message = 'File upload stopped by extension';
                            break;
                        default:
                            $message = 'Unknown upload error';
                    }
                    return $message;
                }
  
                        public function getResult() {
                    return !empty($this -&gt; data) ? $this -&gt; dst : $this -&gt; src;
                }
                        public function getMsg() {
                    return $this -&gt; msg;
                }
                        public function getId() {
                    return $this -&gt; id;
                }
                        public function getTitle() {
                    return  $this-&gt;title;
                }
                        public function getAddress() {
                    return  $this-&gt;address;
                }
                        public function getLot_size() {
                    return  $this-&gt;lot_size;
                }
                        public function getZoning() {
                    return   $this-&gt;zoning;
                }
                        public function getBuild_size() {
                    return  $this-&gt;build_size;
                }
                        public function getSale_price() {
                    return  $this-&gt;sale_price;
                }
                        public function getLease_price() {
                    return  $this-&gt;lease_price;
                }
                        public function getComment() {
                    return  $this-&gt;comment;
                }
                        public function getTransaction() {
                    return  $this-&gt;transaction;
                }
            }
                   $crop = new CropAvatar($_POST['avatar_src'], $_POST['avatar_data'], $_FILES['avatar_file'], $id, $title, $address, $lot_size, $zoning, $build_size, $sale_price, $lease_price, $comment, $transaction);
                   $response = array(
                'state'  =&gt; 200,
                'message' =&gt; $crop -&gt; getMsg(),
                'result' =&gt; $crop -&gt; getResult(),
                'id' =&gt; $crop -&gt; getId(),
                'title' =&gt; $crop -&gt; getTitle(),
                'address' =&gt; $crop -&gt; getAddress(),
                'lot_size' =&gt; $crop -&gt; getLot_size(),
                'zoning' =&gt; $crop -&gt; getZoning(),
                'build_size' =&gt; $crop -&gt; getBuild_size(),
                'sale_price' =&gt; $crop -&gt; getSale_price(),
                'lease_price' =&gt; $crop -&gt; getLease_price(),
                'comment' =&gt; $crop -&gt; getComment(),
                'Transaction' =&gt; $crop -&gt; getTransaction()
            );
            echo json_encode($response);
      ?&gt;
  </code></pre>