Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

tigredonorte

[Resolvido] Erro ao instanciar um objeto

Recommended Posts

Bem, estou fazendo um sistema de notícias utilizando classes e objetos, acontece que ao instanciar um objeto aparece o seguinte erro:

Parse error: parse error, expecting `T_FUNCTION' in F:\Site\xampp\htdocs\Testes\Classes\News.php on line 7

 

se alguém souber o que está acontecendo, agradeço.

 

Segue aqui o código dos arquivos:

 

config.php

<?
/*
	//conexão com o banco do site
	$host_name = "localhost";
	$host_login = "root";
 	$host_senha = "";
	$host_db = "portaltransito";
?>

MySql_Class.php

<?	
include_once("config.php");
class MySql_Class
{
	private $host; 
	private $login; 
	private $password; 
	private $database;
	private $query;
	private $colunas;
//Carrega as informações do config.php
	public function MySql_Class()
	{
		$this->host = $host_name;
		$this->login = $host_login;
		$this->password = $host_senha;
		$this->database = $host_db;
		$this->createDatabase();
	}

	public function conectServer()
	{
		$conexao = mysql_connect("$this->host", "$this->login", "$this->senha") or die("ERRO AO CONECTAR AO SERVIDOR. ERRO = " .mysql_error()); 
	}
	
	public function createDataBase()
	{
		$this->conectServer();
		$this->query = mysql_query("CREATE DATABASE IF NOT EXISTS `$this->database`") or die ("Banco de dados não criado. ERRO = ".mysql_error());
	}
	public function selectDataBase()
	{
		$this->conectServer();
		$db = mysql_select_db("$this->database") or die("ERRO AO ACESSAR O BANCO DE DADOS " .mysql_error());
	}
	public function createTable($table_name, $arguments)
	{
		$this->selectDataBase();
		$this->$query =  mysql_query("CREATE TABLE IF NOT EXISTS `$table_name` ($arguments) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;") or die ("ERRO AO CRIAR TABELA ".mysql_error());
	}

	public function setColunas($colunas)
	{
		$this->selectDataBase();
		$this->colunas = $colunas;	
	}
	public function insertValues($table_name, $values)
	{
		$this->selectDataBase();
		$this->query = mysql_query("INSERT INTO `$table_name` ($this->colunas) VALUES ($values)");	
	}
	public function alterValues($table_name, $variavel, $variavel_valor, $primary, $old_value)
	{
		$this->selectDataBase();
		$this->query = mysql_query("UPDATE `$table_name` SET `$variavel` = '$variavel_valor' WHERE `$primary` = '$old_value'") or die ("ERRO AO ATUALIZAR TABELA : ".mysql_error());	
	}

	public function getValues($coluna, $table_name, $variavel, $variavel_valor)
	{
		$this->selectDataBase();
		$this->query = mysql_query("SELECT $coluna FROM $table_name WHERE $variavel ='$variavel_valor'");
		list($valor) = mysql_fetch_row($this->query);
		return $valor;
	}
	
	public function dropTable($table_name)
	{
		$this->selectDataBase();
		$this->query =  mysql_query("DROP TABLE IF EXISTS `$table_name`") or die ("ERRO AO APAGAR BANCO DE DADOS. ".mysql_error());
	}
	public function dropTableValues($table_name, $primary, $primary_value)
	{
		$this->selectDataBase();
		$this->query =  mysql_query("DELETE FROM `$table_name` WHERE `$primary` = `$primary_value` LIMIT 1;") or die ("ERRO AO APAGAR DADOS.".mysql_error());
	}
	
	public function dropDataBase()
	{
		$this->conectServer();
		$this->query = mysql_query("DROP TABLE IF EXISTS `$this->database`") or die ("Impossível exluir tabela. ERRO = " .mysql_error());
	}
	
}

?>

News.php

<?
	require_once("MySql_Class.php");
	include_once("config.php");
class News
{
//criei um objeto MySql com acesso ao servidor contido em config.php
	$notice_sql = new MySql_Class();
	private $table_name;
	
	private $notice_id;
	private $notice_title;
	private $notice_date;
	private $notice_time;
	private $notice_autor;
	private $notice_conteudo;
	private $notice_indexPage;
		
	private $colunas = "`id`, `title`, `date`, `time`, `autor`, `conteudo`, `index_page`,";
	public function News()
	{
		$table_name = "news";
		$arguments = "`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`title` VARCHAR( 50 ) NOT NULL ,`date` DATE NOT NULL ,`time` TIME NOT NULL, `autor` VARCHAR( 50 ) NOT NULL ,`conteudo` LONGTEXT NOT NULL ,`index_page` TINYINT NOT NULL, `email` TINYINT NOT NULL";		
		$this->notice_sql->createDataBase();
		$this->notice_sql->createTable($this->$table_name, $arguments);
		$this->notice_sql->setColunas($this->$colunas);
	}
	public function createNews($notice_title, $notice_autor, $notice_conteudo, $index_page, $email)
	{
		$this->$notice_date = date("Y-m-d");
		$this->$notice_hour = date("H:i:s");
		$values = "`$notice_title`, `$this->$notice_date`, `$this->$notice_hour`, `$notice_autor`, `$notice_conteudo`, `$index_page`, `$email`";
		$this->notice_sql->insertValues($table_name, $values);	
	}
	public function dropNews($primary_value)
	{
		$this->notice_sql->dropTableValues($this->$table_name, "id", $primary_value)
	}
	public function changeNews($variavel, $variavel_valor ,$old_value)
	{
		$this->notice_sql->alterValues($this->$table_name, $variavel, $variavel_valor, "id", $old_value)	
	}
	public function showNews($id)
	{
		$this->setValues
		(
			$this->notice_sql->getValues('id', $this->table_name, "id", $id),
			$this->notice_sql->getValues('title', $this->table_name, "id", $id),
			$this->notice_sql->getValues('date', $this->table_name, "id", $id),
			$this->notice_sql->getValues('time', $this->table_name, "id", $id),
			$this->notice_sql->getValues('autor', $this->table_name, "id", $id),
			$this->notice_sql->getValues('conteudo', $this->table_name, "id", $id),
			$this->notice_sql->getValues('index_page', $this->table_name, "id", $id)
		);
	}

	public function getId() {return $this->notice_id;}
	public function getTitle(){return $this->notice_title;}
	public function getDate(){return $this->notice_date;}
	public function getTime(){return $this->notice_time;}
	public function getAutor(){return $this->notice_autor;}
	public function getConteudo(){return $this->notice_conteudo;}
	public function getIndex(){return $this->notice_indexPage;}

	private function setValues($id, $title, $date, $time, $autor, $conteudo, $index)
	{
		$this->notice_id = $id;
		$this->notice_title = $title;
		$this->notice_date = $date;
		$this->notice_time = $time;
		$this->notice_autor = $autor;
		$this->notice_conteudo = $conteudo;
		$this->notice_indexPage = $index;
	}

}
?>

inserNoticeInterface.html

 

<html>
<link href="ThomStyle.css" rel="stylesheet" type="text/css">
<body>
<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
	<td height="500" colspan="3" align="center" valign="top"><form name="form1" method="post" action="insert_Notice.php">
	  <table width="800" border="0" cellpadding="0" cellspacing="0">
		<tr>
		  <td width="120" height="25" align="left" valign="middle" class="BannerStyle">Título</td>
		  <td width="500" align="left" valign="middle"><input name="title" type="text" class="TextoReportagem" id="title" size="40" maxlength="50"></td>
		</tr>
		<tr>
		  <td width="120" height="25" align="left" valign="middle" class="BannerStyle">Autor</td>
		  <td align="left" valign="middle"><label>
			<input name="autor" type="text" class="TextoReportagem" id="autor" size="40" maxlength="50">
		  </label></td>
		</tr>
		<tr>
		  <td width="120" height="25" align="left" valign="middle" class="BannerStyle">Página Principal</td>
		  <td align="left" valign="middle"><label>
			<input type="radio" name="main" id="main" value="sim">
			Sim</label>
			<input name="main" type="radio" id="main" value="não" checked>
			Não</td>
		</tr>
		<tr>
		  <td width="120" height="25" align="left" valign="middle" class="BannerStyle">Enviar por Email</td>
		  <td align="left" valign="middle"><label>
			<input type="radio" name="email" id="email" value="sim">
			Sim</label>
			<input name="email" type="radio" id="email" value="não" checked>
			Não</td>
		</tr>
		<tr>
		  <td width="120" align="left" valign="top" class="BannerStyle">Conteúdo</td>
		  <td align="left" valign="middle"><textarea name="conteudo" cols="80" rows="20" class="TextoReportagem" id="conteudo"></textarea></td>
		</tr>
		<tr>
		  <td height="28" colspan="2" align="center" valign="middle"><label>
			<input name="Postar" type="submit" class="TituloVendas" id="Postar" value="Enviar">
		  </label></td>
		</tr>
	  </table>
	</form></td>
  </tr>
</table>
</body>
</html>

insert_Notice.php

 

<?
	include_once("site_options.php");
	include_once("MySql_Class.php");
	include_once("News.php");
	
	$title_post = $_POST[title];
	$autor_post = $_POST[autor];
	$conteudo_post = $_POST[conteudo];
	$main_post = $_POST[main];
	$email_post = $_POST[email];
	
	$notice_news = new News();
	$notice_news->createNews($title_post, $autor_post, $conteudo_post, $main_post, $email_post);
	$MSG = "Notícia postada corretamente";
	header('./');
?>
<table width="200" border="1">
  <tr>
	<td height="67" colspan="3"><? echo $MSG?></td>
  </tr>
</table>

Bem.. é isto, se algém puder me ajudar, agradeço

Compartilhar este post


Link para o post
Compartilhar em outros sites

Instancie o objeto fora da nova classe.

E coloque ponto e vírgula no final dos comandos.

News.php

public function dropNews($primary_value)
	{
		$this->notice_sql->dropTableValues($this->$table_name, "id", $primary_value);
	}
	public function changeNews($variavel, $variavel_valor ,$old_value)
	{
		$this->notice_sql->alterValues($this->$table_name, $variavel, $variavel_valor, "id", $old_value);
	}

Compartilhar este post


Link para o post
Compartilhar em outros sites

$notice_sql = new MySql_Class();
isso está errado

 

Você não pode instanciar uma classe, ou chamar uma função, onde declara as propriedades dela (variáveis de classe).

Tem que instanciar dentro de um método. Pode ser um método construtor, por exemplo.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.