PHP OOP variable scope problem
12-29-2010, 11:26 AM, (This post was last modified: 12-29-2010, 11:31 AM by RichardGv.)
#4
RE: PHP OOP variable scope problem
(12-29-2010, 12:59 AM)Vanilla Wrote: Regarding the static thing, I don't know why PHP doesn't print error messages. I've set php to display all errors in the .ini file.
It's probably a windows fault.

Hmm, no, it isn't a Windows fault, I just found out. E_STRICT Strict Standard warnings are not enabled by default in PHP. I have it enabled since XAMPP does by default.
To enable E_STRICT warnings, change error_reporting in php.ini to:
Code:
error_reporting = E_ALL | E_STRICT

(12-29-2010, 12:59 AM)Vanilla Wrote: Anyway, I was just wondering. If I had a lot of properties/vars, would it be feasible to load them onto an array, and then pass the array as an argument (To Activate())? Would that be a valid way to do such without generating errors?

  1. In PHP syntax it's perfectly valid.
  2. However it does not sound like a good idea: Packing all the variables to an array will require a lot of extra code, more than even writing all the parameters down.
    Let's imagine packing 3 variables:
    PHP Code:
    // Overall: 335 bytes of code
    // main.php - Packing part
    $packedvars = array('variableOne''variableTwo''variableThree');
    for 
    each($packedvars as $varname)
        
    $pack[$varname] = $$varname;
    Activate("the Oven"$pack);

    // child.php - Unpacking part
    function Activate($Arg$pack) {
        echo 
    $pack['variableOne'];
        echo 
    $pack['variableTwo'];
        echo 
    $pack['variableThree'];

    But if you just write all the variables directly to the parameter list:
    (There's one point to pay attention: You may copy a large part of the first line in main.php directly to the first line in child.php).
    PHP Code:
    // Overall: 253 bytes of code
    // main.php - Calling part
    Activate("the Oven"$variableOne$variableTwo$variableThree);

    // child.php
    function Activate($Arg$variableOne$variableTwo$variableThree) {
        echo 
    $variableOne;
        echo 
    $variableTwo;
        echo 
    $variableThree;

    So, actually, packing all the variables requires you to type more code! Not to mention the memory and CPU time cost of duplicating every variable for 3 times (the first time as $variableOne in main.php, the second time as $pack['variableOne'] in main.php, the third time as $pack['variableOne'] in child.php). Worthy? Not really.
    Passing the whole object (as reference) could be even easier, if possible.
Gentoo Linux User (w/ fvwm) / Loyal Firefox User / Owner of a Stupid Old Computer - My PGP Public Key

No man is an island, entire of itself; every man is a piece of the continent, a part of the main; if a clod be washed away by the sea, Europe is the less, as well as if a promontory were, as well as if a manor of thy friends or of thine own were; any man's death diminishes me, because I am involved in mankind; and therefore never send to know for whom the bell tolls; it tolls for thee.
-- Devotions Upon Emergent Occasions (1624), John Donn


Messages In This Thread
PHP OOP variable scope problem - by Vanilla - 12-28-2010, 08:10 AM
RE: PHP OOP variable scope problem - by RichardGv - 12-28-2010, 04:12 PM
RE: PHP OOP variable scope problem - by Vanilla - 12-29-2010, 12:59 AM
RE: PHP OOP variable scope problem - by RichardGv - 12-29-2010, 11:26 AM
RE: PHP OOP variable scope problem - by Vanilla - 12-30-2010, 02:20 AM
RE: PHP OOP variable scope problem - by Vanilla - 01-03-2011, 09:09 AM

Forum Jump: