You are not logged in.

#1 2004-12-26 09:54:24

dp
Member
From: Zürich, Switzerland
Registered: 2003-05-27
Posts: 3,378
Website

help! my code only works under php5 (archlinux)

somehow, one piece of code i wrote for a menu-generation does only work on my machine (running php5 on archlinux) ... it does work as well on redhat at the university, but it does not do the same

if you know php and especially know the difference in php4 and php5, then please have a look at this chaos:

<?

class menuentry{

    var $name;
    var $short;
    var $linksSubmenu;
    var $isTitle;
    var $submenu;

    function menuentry($Tshort, $Tname, $TisTitle){
            $this->short = $Tshort;
            $this->name = $Tname;
            $this->linksSubmenu = 0;
            $this->isTitle = $TisTitle;
    }

        function addSubmenu($submenu){
            $this->submenu = $submenu;
            $this->linksSubmenu = 1;
        }

        function linksSubmenu(){
            return $this->linksSubmenu;
        }
}

class menu
{
    var $entries;

    function addEntryT($short, $name){ // Titel
        $this->entries[count($this->entries)] = & new menuentry($short, $name, 1);
    }

    function addEntry($short, $name){ // normal
        $this->entries[count($this->entries)] = & new menuentry($short, $name, 0);
    }

    function getEntry($short){
        $liste = $this->entries;
        for($a = 0; $a < count($liste); $a++) {
            if($liste[$a]->short==$short){
                return $liste[$a];
            }
        }
    }
}


function MenuAusgabe($menu, $baseurl){

$alles=$menu->entries;

    for($i=0; $i<count($alles); $i++){

        if($alles[$i]->isTitle==1){ // TITEL
            echo "<div>".$alles[$i]->name."</div>n";
        }
        elseif($alles[$i]->short==$_GET['show']){ // AKTIV
            echo "t<a class='aktiv' href='".$baseurl."?show=".$alles[$i]->short."'>".
                $alles[$i]->name."</a> n";
            if($alles[$i]->linksSubmenu==1){ // SUBMENU EXISTIERT!
                $submenu=$alles[$i]->submenu;
                $hermitdendaten=$submenu->entries;
                for($s=0; $s<count($hermitdendaten); $s++){
                    if($hermitdendaten[$s]->short==$_GET['subcat']){
                        echo "tt<a class='aktivsubmenu' href='".$baseurl."?show=".$alles[$i]->short."&subcat=".$hermitdendaten[$s]->short."'>".$hermitdendaten[$s]->name."</a> n";
                    }else{
                        echo "tt<a class='submenu' href='".$baseurl."?show=".$alles[$i]->short."&subcat=".$hermitdendaten[$s]->short."'>".$hermitdendaten[$s]->name."</a> n";
                    }
                }
            }
        }else{ // NICHT AKTIV
            echo "t<a href='".$baseurl."?show=".$alles[$i]->short."'>".
                $alles[$i]->name."</a> n";
        }

    }

}


?>
<!-- MENU ---------------------------- MENU -->

<?

/* alles für das menu ist unter functions/menu.php deklariert */

define('FUNCTIONS_DIR', '/home/httpd/vhosts/calcutta-project.ch/httpdocs/functions/');
require_once(FUNCTIONS_DIR.'menu.php');


/* Build Menu: */
$hauptmenu = new menu;

$hauptmenu->addEntryT("titel1", "Calcutta Project Basel");
$hauptmenu->addEntry("partner", "Partner");
$hauptmenu->addEntry("sponsoren", "Sponsors");

$hauptmenu->addEntryT("titel2", "NGO's");
$hauptmenu->addEntry("ngosCH", "Switzerland");
    $Eintrag_ngosCH = $hauptmenu->getEntry("ngosCH");
        $ngosCH = new menu;
        $ngosCH->addEntry("umwelt", "Environnement");
        $ngosCH->addEntry("armut", "Poverty");
    $Eintrag_ngosCH->addSubmenu($ngosCH);
$hauptmenu->addEntry("ngosE", "World");
    $Eintrag_ngosE = $hauptmenu->getEntry("ngosE");
        $ngosE = new menu;
        $ngosE->addEntry("umwelt", "Environnement");
        $ngosE->addEntry("armut", "Poverty");
    $Eintrag_ngosE->addSubmenu($ngosE);

$hauptmenu->addEntryT("titel3", "India");
$hauptmenu->addEntry("calcutta", "Calcutta");
$hauptmenu->addEntry("india", "General");

/*
echo "<pre>";
echo print_r($hauptmenu);
echo "</pre>";
*/

/* WRITE MENU */
echo "<div class='sideBox LHS'>";

MenuAusgabe($hauptmenu, "/pub/links.en.php");

echo "</div>";

?>


<!-- MENU ---------------------------- MENU -->

THE DIFFERENCE IN EXECUTION:

    $Eintrag_ngosE = $hauptmenu->getEntry("ngosE");
        $ngosE = new menu;
        $ngosE->addEntry("umwelt", "Environnement");
        $ngosE->addEntry("armut", "Poverty");
    $Eintrag_ngosE->addSubmenu($ngosE);

or better:

    function getEntry($short){ 
         $liste = $this->entries; 
         for($a = 0; $a < count($liste); $a++) { 
             if($liste[$a]->short==$short){ 
                 return $liste[$a]; 
             } 
         } 
     } 

is only done under php5 (archlinux) correctly (in redhat ... running php4, it is simply ignored (no error, no warning, no nothing ... but also no submenus!))

any ideas, how can i make my code php4 compatible? (i searched the net, but there are only solutions how to make your php4-code working under php5 and not the other way round - :twisted:  :evil:  :twisted: )

any input welcome  8)  lol


The impossible missions are the only ones which succeed.

Offline

#2 2004-12-26 10:57:46

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: help! my code only works under php5 (archlinux)

at first glance, and without more examples (hows that for virtual debugging..lol)
I would say that it is likely coming down to one of the main differences between php4 and php5..that values in php4 are passed by value, and in php5 are passed by reference.

Prior to php5, when you assigned an object to a variable, or passed it to a function, it was actually copied into the variable or function. php5 uses the zend engine2, which copies by reference, rather than copying. This means that in php5 if several variables point to the same object, and you update the object, each variable can access the updated object contents. In php4, each variable would simply have a copy of the outdated object.

I would venture a guess that likely that is the main difference that you are experiencing trying to revert back to php4..

Just an educated guess though...I will look at the code a bit more and see..


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2004-12-26 11:05:42

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: help! my code only works under php5 (archlinux)

also in

function addEntryT($short, $name){ // Titel
        $this->entries[count($this->entries)] = & new menuentry($short, $name, 1);
    } 

you are using references..
see here: http://us4.php.net/manual/en/language.references.php

namely the first two comments (newest on top)...they appear to be related to your issue.

and I quote.."Re-using variables which were references before, without unsetting them first, leads to unexpected behaviour."

are you experiencing "unexplained behavior"?
wink


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

Board footer

Powered by FluxBB