Visualizzazione dei risultati da 1 a 5 su 5

Discussione: function php

  1. #1

    function php

    Ho qusta funzione in php5..il problema è che deve girare su php4

    function w() {
    if($this->sons == NULL)
    return 1;

    $sum = 0;

    for($i=0; $i<$this->sons->countSons(); $i++) {
    $sum += $this->sons->at($i)->w();
    }

    return $sum;
    }

    quando lancio la pagina mi stampa il seguente errore
    Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /web/htdocs/www.coopsse.it/home/magdapesce/tree/Node.php on line 31

    C'è un sistema per adattarla a php4?
    GRAZIE!

  2. #2
    Sostituisci $this-> con il semplice $

  3. #3
    ho provato ma da sempre lo stesso errore alla stessa riga...

  4. #4
    w() non è una funzione ma un metodo di una classe. Non puoi utilizzarla a se stante senza il resto della classe perchè contiene riferimenti alla classe da cui l'hai estratta (es. $this).

  5. #5
    posto le tre pagine, senza le modifiche, che formano il mio script... questo script serve per generare una sorta di diagramma di flusso.

    -------------------------------------------------------------------------------
    pagina uno: Node.php


    <?php
    require_once("NodeList.php");

    class Node {
    var $content; // int char string...
    var $sons; // NodeList

    function __construct($c) {
    $content = $c; // int char string ...
    $sons = NULL; // NodeList
    }

    function addSon($son) { // $son : Node
    if($this->sons == NULL) {
    $this->sons = new NodeList($son);
    $this->sons->content = $son;
    $this->sons->next = NULL;
    return;
    }

    $this->sons->add($son);
    }

    function w() {
    if($this->sons == NULL)
    return 1;

    $sum = 0;

    for($i=0; $i<$this->sons->countSons(); $i++) {
    $sum += $this->sons->at($i)->w();
    }

    return $sum;
    }

    function h() {
    if($this->sons == NULL)
    return 1;

    $max = 0;
    for($i=0; $i<($this->sons->countSons()); $i++) {
    $sonh = 1+$this->sons->at($i)->h();
    if($sonh > $max)
    $max = $sonh;
    }

    return $max;
    }

    function printNode() {
    echo "".$this->content." w:".$this->w()." h:".$this->h()."
    ";

    if($this->sons != NULL) {
    for($i=0; $i<($this->sons->countSons()); $i++) {
    $this->sons->at($i)->printNode();
    }
    }
    }

    }
    ?>
    ----------------------------------------------------------------------------------------
    pagina 2: NodeList.php

    <?php
    require_once("Node.php");

    class NodeList {
    var $content; // : Node
    var $next; // : NodeList

    function __construct($c) {
    $this->content = $c;
    $this->next = NULL;
    }

    function at($i) { // : Node
    if($i==0)
    return $this->content;

    if($this->next == NULL)
    return NULL;

    $i--;

    return $this->next->at($i);
    }

    function add($n) { // $n : Node
    if($this->next==NULL) {
    $this->next = new NodeList($n);
    $this->next->content = $n;
    $this->next->next = NULL;
    //$n->next = NULL;
    return;
    }

    return $this->next->add($n);
    }

    function remove($n) {
    // TODO
    }

    function countSons() {
    if($this->next == NULL)
    return 1;

    return 1+($this->next->countSons());
    }
    }
    ?>

    ----------------------------------------------------------------------------------------

    pagina 3: index.php

    <html>
    <head>
    <title>Tree</title>
    </head>

    <body>
    <?php
    require_once("Node.php");

    echo "Output

    ";

    $a = new Node(0);
    $a->content = 'A';

    $b = new Node(0);
    $b->content = 'B';
    $a->addSon($b);

    $e = new Node(0);
    $e->content = 'E';
    $b->addSon($e);

    $c = new Node(0);
    $c->content = 'C';
    $a->addSon($c);

    $d = new Node(0);
    $d->content = 'D';
    $a->addSon($d);

    $f = new Node(0);
    $f->content = 'F';
    $d->addSon($f);

    $g = new Node(0);
    $g->content = 'G';
    $d->addSon($g);

    $h = new Node(0);
    $h->content = 'H';
    $g->addSon($h);


    $root = $a;
    $root->printNode();

    function getStyle($color, $w, $ml) {
    echo "position:absolute; border:1px solid #000; padding:3px; margin:3px; text-align:center; width:$w; background:$color; margin-left: $ml;";
    }
    ?>




    <h3>Immagine con un albero di esempio</h3>

    </p>




    <h3>Rappresentazione dell'albero</h3>
    <div style="<?=getStyle('#ccf', '800px', '0%');?>">
    A
    <div style="diplay:inline; <?=getStyle('#aac', '22%', '0%');?>">
    B
    <div style="<?=getStyle('#99a', '90%', '0%');?>">
    E
    </div>
    </div>
    <div style="diplay:inline; <?=getStyle('#aac', '22%', '25%');?>">
    C
    </div>
    <div style="diplay:inline; <?=getStyle('#aac', '44%', '50%');?>">
    D
    <div style="diplay:inline; <?=getStyle('#99a', '44%', '0%');?>">
    F
    </div>
    <div style="diplay:inline; <?=getStyle('#99a', '44%', '50%');?>">
    G
    </div>
    </div>
    </div>
    </p>

    </body>
    </html>

    -------------------------------------------------------------------------------

    E' in php5 ma non posso utilizzarlo perchè devo metterlo su aruba ed aruba non supporta il php5 ma solo il 4.
    L'errore me lo da nella pagina 1 in questo punto


    function w() {
    if($this->sons == NULL)
    return 1;

    $sum = 0;

    for($i=0; $i<$this->sons->countSons(); $i++) {
    $sum += $this->sons->at($i)->w();
    }

    return $sum;
    }

    ed è questo:

    Parse error: parse error in c:\programmi\easyphp1-8\www\magda\tree\Node.php on line 31

    Linea 31: $sum += $this->sons->at($i)->w();

    Grazie a tutti!
    Chi mi aiuta ad adattarlo?! sempre se è possibile...ma spero tanto di si!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.