php Fatal error: Cannot access empty property in line 37

Schlumpf90

Well-known member
22 Mai 2008
225
5
Ich habe gerade Kopf Schmerzen und komme nicht weiter x.X

PHP:
#########################################################################################

class Template
{
    var $template_source_array = array();

    #########################################################################################
    /* function main() :
     *
     * needs   : filename : $file
     *
     * returns : 1 if successful
     */
    function main($file)
    {
        global $template_source_array;
        $this -> $template_source_array = file($file) or die("couldn't open file: $file");
        return(1);
    }

    #########################################################################################
    /* function code() :
     *
     * needs   : $input : array with the replacements
     *
     * returns : 1 is successful
     */
    function code($input)
    {
        global $template_source_array;
        $code_source = $this -> $template_source_array;
        $x = 0;
        foreach ( $code_source as $code_row )
        {
            # replaces the <!--php's
            while (strpos($code_row, "<!--php:") > -1)
            {
                $code_row = $this -> replace_php($code_row, $input);
            }
            $code_source[$x] = $code_row;
            $x++;
        }

        $new_template = $this -> extract_loop_source($code_source, $input);

        # writes all back to main template
        $this -> $template_source_array = $new_template;
        return(1);
    }

    #########################################################################################
    /* function t_print() :
     *
     * prints the template
     */
    function t_print()
    {
        global $template_source_array;
        print join("",$this -> $template_source_array);
    }

    #########################################################################################
    /* function return_file() :
     *
     * writes the template to a given filename
     */
    function write_file($filename)
    {
        global $template_source_array;
        $fp = fopen ($filename, "w");
        fwrite($fp, join("",$this -> $template_source_array)) or die("Konnte Datei nicht schreiben");
        return(1);
    }

    #########################################################################################
    function return_template()
    {
        global $template_source_array;
        return(join("",$this -> $template_source_array));
    }


    #########################################################################################
    #########################################################################################
    /*
     * CLASS NEEDED FUNCTIONS
     */

    /* function replace_php() :
     *
     * needs a single html code row :           $code_row
     *       the array with the replacements :  $input
     *
     * returns :
     * the replaced row
     */
    function replace_php($code_row, $input)
    {
        # gets the variable name
        $pos1 = strpos($code_row, "<!--php:");
        $pos2 = strpos($code_row, "-->");
        $key  = substr($code_row, $pos1 + 8, $pos2 - 9 - $pos1);
        # removes whithespaces
        $key  = trim($key);

        #replaces this
        if(isset($input{$key}))
        {
            $input{$key} = "".$input{$key};            # casts var as string
        }else
        {
            $input{$key} = "";
        }
        $code_row = eregi_replace("<!--php:( )*$key( )*-->", $input{$key}, $code_row );
        return($code_row);
    }

    #########################################################################################
    /* function do_loop() :
     *
     * needs $loop_source: html source with the comments to do a loop
     *       $loop_name  : name of the loop ( <!--php_start: name --> )
     *       $input      : array{loop_name} = array of hashes to to the loop, if not set nothin is returned
     *
     * returns :
     * array with html code rows
     */
    function do_loop($loop_source, $loop_name, $input)
    {
        $return = "";
        if(is_array($input{$loop_name}))
        {
            # removes the comments
            array_shift($loop_source);
            array_pop($loop_source);

            $row_template = join("\n", $loop_source);
            if(isset($input{$loop_name}))
            {
                $x = 0;
                foreach($input{$loop_name} as $row_array)
                {
                    # checks for other loops
                    $row_template = join("\n", $this -> extract_loop_source($loop_source, $input{$loop_name}[$x]));

                    $temp_row = $row_template;
                    if(is_array($row_array))
                    {
                        foreach($row_array as $key => $value)
                        {
                            $temp_row = str_replace("[$key]", $value, $temp_row );
                        }
                    }
                    $x++;
                    $return .= $temp_row;
                }
            }
            $return_array = explode("\n", $return);
        }
        return($return_array);
    }

    #########################################################################################
    /* function extract_loop_source() :
     *
     * needs html source:                $code_source
     *       array with the replacments: $input
     *
     * returns :
     * returns the replaced html code source
     */
    function extract_loop_source($code_source, $input)
    {
        $x = 0;
        $start = 0;
        $loop_name = "";
        $new_template = array();
        foreach ( $code_source as $code_row )
        {
            if(strpos($code_row, "<!--php_start: $loop_name") > -1)
            {
                $start = 1;
                # gets name of loop
                $pos1 = strpos($code_row, "<!--php_start:");
                $pos2 = strpos($code_row, "-->");
                $loop_name = substr($code_row, $pos1 + 15, $pos2 - $pos1 - 16);
            }

            # sets a start flag
            if($start == 1)
            {
                if(!(isset($loop_source)))
                {
                    $loop_source = array();
                }
                array_push($loop_source, $code_row);
            }
            else
            {
                array_push($new_template, $code_row);
            }

            if(strpos($code_row, "<!--php_end: $loop_name") > -1)
            {
                $hole_loop = $this -> do_loop($loop_source, $loop_name, $input);
                # writes the loop back to the main template
                if(is_array($hole_loop))
                {
                    foreach ($hole_loop as $loop_row) { array_push($new_template, $loop_row); }
                }
                unset($loop_source);
                $loop_name = "";
                $start = 0;
            }
            $x++;
        }
        if($start == 1) { die("never ending loop found: \"$loop_name\""); }
        return($new_template);
    }
}
?>

Wäre nett für hilfe villeicht findet ihr den fehler
 
Zuletzt bearbeitet von einem Moderator:
Die global-Anweisung muss/(sollte) auch überall raus, weil du ja immer im Klassenkontext bist und auf nichts von außen zugreifst.

P.S. PHP-Tags benutzen ;)
 
Kannst du mir den Code verändert posten so das er funkt kriegst5 mio lose
PHP:
#########################################################################################

class Template
{
	var $template_source_array = array();

	#########################################################################################
	/* function main() :
	 *
	 * needs   : filename : $file
	 *
	 * returns : 1 if successful
	 */
	function main($file)
	{
		global $template_source_array;
		$this -> $template_source_array = file($file) or die("couldn't open file: $file");
		return(1);
	}

	#########################################################################################
	/* function code() :
	 *
	 * needs   : $input : array with the replacements
	 *
	 * returns : 1 is successful
	 */
	function code($input)
	{
		global $template_source_array;
		$code_source = $this -> $template_source_array;
		$x = 0;
		foreach ( $code_source as $code_row )
		{
			# replaces the <!--php's
			while (strpos($code_row, "<!--php:") > -1)
			{
				$code_row = $this -> replace_php($code_row, $input);
			}
			$code_source[$x] = $code_row;
			$x++;
		}

		$new_template = $this -> extract_loop_source($code_source, $input);

		# writes all back to main template
		$this -> $template_source_array = $new_template;
		return(1);
	}

	#########################################################################################
	/* function t_print() :
	 *
	 * prints the template
	 */
	function t_print()
	{
		global $template_source_array;
		print join("",$this -> $template_source_array);
	}

	#########################################################################################
	/* function return_file() :
	 *
	 * writes the template to a given filename
	 */
	function write_file($filename)
	{
		global $template_source_array;
		$fp = fopen ($filename, "w");
		fwrite($fp, join("",$this -> $template_source_array)) or die("Konnte Datei nicht schreiben");
		return(1);
	}

	#########################################################################################
	function return_template()
	{
		global $template_source_array;
		return(join("",$this -> $template_source_array));
	}


	#########################################################################################
	#########################################################################################
	/*
	 * CLASS NEEDED FUNCTIONS
	 */

	/* function replace_php() :
	 *
	 * needs a single html code row :           $code_row
	 *       the array with the replacements :  $input
	 *
	 * returns :
	 * the replaced row
	 */
	function replace_php($code_row, $input)
	{
		# gets the variable name
		$pos1 = strpos($code_row, "<!--php:");
		$pos2 = strpos($code_row, "-->");
		$key  = substr($code_row, $pos1 + 8, $pos2 - 9 - $pos1);
		# removes whithespaces
		$key  = trim($key);

		#replaces this
		if(isset($input{$key}))
		{
			$input{$key} = "".$input{$key};			# casts var as string
		}else
		{
			$input{$key} = "";
		}
		$code_row = eregi_replace("<!--php:( )*$key( )*-->", $input{$key}, $code_row );
		return($code_row);
	}

	#########################################################################################
	/* function do_loop() :
	 *
	 * needs $loop_source: html source with the comments to do a loop
	 *       $loop_name  : name of the loop ( <!--php_start: name --> )
	 *       $input      : array{loop_name} = array of hashes to to the loop, if not set nothin is returned
	 *
	 * returns :
	 * array with html code rows
	 */
	function do_loop($loop_source, $loop_name, $input)
	{
		$return = "";
		if(is_array($input{$loop_name}))
		{
			# removes the comments
			array_shift($loop_source);
			array_pop($loop_source);

			$row_template = join("\n", $loop_source);
			if(isset($input{$loop_name}))
			{
				$x = 0;
				foreach($input{$loop_name} as $row_array)
				{
					# checks for other loops
					$row_template = join("\n", $this -> extract_loop_source($loop_source, $input{$loop_name}[$x]));

					$temp_row = $row_template;
					if(is_array($row_array))
					{
						foreach($row_array as $key => $value)
						{
							$temp_row = str_replace("[$key]", $value, $temp_row );
						}
					}
					$x++;
					$return .= $temp_row;
				}
			}
			$return_array = explode("\n", $return);
		}
		return($return_array);
	}

	#########################################################################################
	/* function extract_loop_source() :
	 *
	 * needs html source:                $code_source
	 *       array with the replacments: $input
	 *
	 * returns :
	 * returns the replaced html code source
	 */
	function extract_loop_source($code_source, $input)
	{
		$x = 0;
		$start = 0;
		$loop_name = "";
		$new_template = array();
		foreach ( $code_source as $code_row )
		{
			if(strpos($code_row, "<!--php_start: $loop_name") > -1)
			{
				$start = 1;
				# gets name of loop
				$pos1 = strpos($code_row, "<!--php_start:");
				$pos2 = strpos($code_row, "-->");
				$loop_name = substr($code_row, $pos1 + 15, $pos2 - $pos1 - 16);
			}

			# sets a start flag
			if($start == 1)
			{
				if(!(isset($loop_source)))
				{
					$loop_source = array();
				}
				array_push($loop_source, $code_row);
			}
			else
			{
				array_push($new_template, $code_row);
			}

			if(strpos($code_row, "<!--php_end: $loop_name") > -1)
			{
				$hole_loop = $this -> do_loop($loop_source, $loop_name, $input);
				# writes the loop back to the main template
				if(is_array($hole_loop))
				{
					foreach ($hole_loop as $loop_row) { array_push($new_template, $loop_row); }
				}
				unset($loop_source);
				$loop_name = "";
				$start = 0;
			}
			$x++;
		}
		if($start == 1) { die("never ending loop found: \"$loop_name\""); }
		return($new_template);
	}
}
?>
 
Du musst nur die globals entfernen.
Du kannst innerhalb einer Klasse mit $this-> auf alles zugreifen.
Statt "global $template_source_array;" kannst du $this->template_source_array nutzen

Und in zeile 31 änderst du $this->$template_source_array in $this->template_source_array ab.
Hinter dem $this-> brauchst du kein $ mehr.
 
Du musst nur die globals entfernen.
Du kannst innerhalb einer Klasse mit $this-> auf alles zugreifen.
Statt "global $template_source_array;" kannst du $this->template_source_array nutzen

Und in zeile 31 änderst du $this->$template_source_array in $this->template_source_array ab.
Hinter dem $this-> brauchst du kein $ mehr.



Habs soweit geändert https://web2346.terra-hosting.de/

Fehler bleibt bestehen
 
Entferne mal die ganzen Leerzeichen bei deinen $this->
Da steht z.B. "$this -> $template_source_array;", also ein leerzeichen vor dem -> und eines danach. mach die mal im gesamten Quelltext weg, versuch es dann nochmal und poste ggf. danach nochmal den gesamten Quelltext hier ;-)

Im übrigen musst du JEDES $this -> $template_source_array in $this-> template_source_array im Quelltext ändern. Du verwendest es ja mehrmals falsch ;-)
 
Nun tratt folgender fehler auf

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in /var/www/web2346/html/includes/template.php on line 23