0

I know this is common error and I have read all answer but my case looks different, at least as a PHP newbie so kindly don't decide action based on question subject.

I have a PHP code which working but I am trying to arrange it in appropriate functions, as in below code but I get following error Warning: Creating default object from empty value in /home/abc/vhosts/localhost/public/portfolio.php on line 16:

<?php

    // configuration
    require("../includes/config.php"); 

    // prepare portfolio object
    $portfolioObject = (object) ["portfolioSummaryArr" => [], "cashInHand" => ""];

    function getCashInHand(){
        // query database to get cash in hand
        $rows = CS50::query("SELECT cash FROM users WHERE username = ?", $_SESSION["username"]);

        // get first (and only) row
        $row = $rows[0];

        $portfolioObject->cashInHand = $row["cash"];
        $portfolioObject->portfolioSummaryArr = [11, 22, 33, 44, 55];
    }

    getCashInHand();

?>

Now after reading all the answers and documentation I think I need some sort of object first so I did this and then I get this error Parse error: syntax error, unexpected '$myObject' (T_VARIABLE) in /home/abc/vhosts/localhost/public/portfolio.php on line 12. I trusted this because something similar is allowed in JS and it works, but guess in PHP isn't so my guess is that using objects created like this you cannot define or access function.

$myObject = new stdClass();

$myObject->demoFunction = function(){
    echo "I am coming from demo function.";
}

$myObject->demoFunction(); 

To me things get more interesting because if I do as in below in my code just after require("../includes/config.php"); then it works and no error, but function definition and access as shown in my first code snippet doesn't work.

// Defining function
function whatIsToday(){
    echo "Today is XXX";
}
// Calling function
whatIsToday();
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • 1
    the short answer is: variable scope. –  Apr 10 '16 at 02:02
  • @Dagon Thanks for the hint. I created `$myObject = new stdClass();` a global object and then was defining `portfolioObject` as `$myObject->portfolioObject = (object) ["portfolioSummaryArr" => [], "cashInHand" => ""];` and accessing as `$myObject->portfolioObject->cashInHand = $row["cash"];` but still same error. I guess now I have access to `portfolioObject` through my global `myObject` object? – hagrawal7777 Apr 10 '16 at 02:21
  • 1
    Review [this answer](http://stackoverflow.com/questions/2938004/how-to-add-a-new-method-to-a-php-object-on-the-fly) for info on how to call your method. But the `unexpected $myobject` is due to a missing `;` after the function definition. In a normal function def, you would not have a terminating `;` after `}` but yours is a variable assignment of an anonymous function. That is a statement for which PHP requires a `;` here... `$myObject->demoFunction = function(){...};` <--- – Michael Berkowski Apr 10 '16 at 02:38
  • 1
    Also, if you declare you arrays using `array(x,y,z,...)` instead of bracket notation, you shouldn't have to do any of the `(object)` stuff. – Matthew Herbst Apr 10 '16 at 02:40
  • @Dagon Thank you sir for the initial hint, I got it and posted my answer for future visitors. – hagrawal7777 Apr 10 '16 at 04:04
  • @MatthewHerbst Thanks mate, I guess I needed more cleaner approach, I got it now and posted my answer for future visitors. – hagrawal7777 Apr 10 '16 at 04:05

1 Answers1

0

It was just matter of time, I would say. I had to do some reading to understand leverage the object orientation of PHP. Below is how I could resolve all my PHP errors and warnings. Needless to say my favorite debugging methodology "print to screen/terminal", in case of PHP using echo helped a lot.

<?php

    // configuration
    require("../includes/config.php"); 

    class portfolio{
        public $portfolioObject;

        public function __construct(){
            echo "__construct()</br>";
            $this->portfolioObject = new stdClass(); 
        }

        public function initialize(){
            echo "initialize()";
            $portfolioObject = (object) ["portfolioSummaryArr" => [], "cashInHand" => ""];
        }

        public function getCashInHand(){
            // query database to get cash in hand
            $rows = CS50::query("SELECT cash FROM users WHERE username = ?", $_SESSION["username"]);

            // get first (and only) row
            $row = $rows[0];

            $this->portfolioObject->cashInHand = $row["cash"];
            $this->portfolioObject->portfolioSummaryArr = [11, 22, 33, 44, 55];
        }

        public function setSessionObject(){
            $_SESSION["portfolioSummaryResults"] = $this->portfolioObject;
        }     
    }

    $myPortfolio = new portfolio();
    $myPortfolio->initialize();
    $myPortfolio->getCashInHand();
    $myPortfolio->setSessionObject();

    render("portfolio/portfolio_results.php", ["title" => "Portfolio"]);

?>
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70