'Unable to start class in php
I'm doing a PHP form with ajax but having trouble initializing a class ou classes. I think the problem is with the path, I have a addproduct.php with this form, i don't believe posting the form will help beacause it isnt getting any error, but if you think it is, let me know so i can post it.
This is where i submit the form (posproduct.js)
$(document).on('submit', '#product_form', function(){
var $form = $('#product_form');
var dados = $form.serialize();
$.ajax({
type: 'POST',
url: 'form.php',
async: true,
dataType: "html",
data: dados
}).done(function(msg){
window.location.href = "index.php";
}).fail(function(jqXHR, textStatus, msg){
alert(dados);
console.log(jqXHR, textStatus, msg);
});
return false;
});
Here i enter in this try catch but dont get this error, only if i take out the "//" (form.php)
<?php
//require_once("config.php");
include_once('/class/Book.php');
include_once('/class/DVD.php');
include_once('/class/Forniture.php');
saveProduct();
function saveProduct(){
$ok = 0;
try {
//http_response_code(405);
$book = new Book();
http_response_code(405);
$book->setDescription($size);
$book->setSku($sku);
$book->setName($name);
$book->setPrice($price);
$book->setType($type);
$book->insert();
http_response_code(401);
//$ok += 1;
} catch (Exception $e) {
//echo "$e";
}
My Book Class
<?php
include_once('/class/Product.php');
class Book extends Product
{
public function setDescription(int $value)
{
$this->description = $value;
}
}
?>
My product class
<?php
include_once('DAO.php');
abstract class Product
{
protected $sku;
protected $name;
protected $price;
protected $type;
protected $description;
public function getSku()
{
return $this->sku;
}
public function setSku($value)
{
$this->sku = $value;
}
public function getName(){
return $this->name;
}
public function setName($value)
{
$this->name = $value;
}
public function getPrice()
{
return $this->price;
}
public function setPrice($value)
{
$this->price = $value;
}
public function getType()
{
return $this->type;
}
public function setType($value)
{
$this->type = $value;
}
public function getDescription()
{
return $this->description;
}
abstract public function setDescription($value);
public function insert()
{
$dao = new Dao();
$result = $dao->query("INSERT INTO tb_produtos (sku, pname, price, ptype, pdescription) VALUES(:SKU, :NAME, :PRICE, :TYPE, :DESCRIPTION)", array(
":SKU"=> $this->getSku(),
":NAME"=> $this->getPname(),
":PRICE"=> $this->getPrice(),
":TYPE"=> $this->getType(),
":DESCRIPTION"=> $this->getDescription()
));
}
public function getList()
{
$dao = new Dao();
return $dao->select("SELECT * FROM tb_produtos ORDER BY id;");
}
public function __toString()
{
return json_encode(array(
"sku"=>$this->getSku(),
"name"=>$this->getName(),
"price"=>$this->getPrice(),
"type"=>$this->getType(),
"description"=>$this->getDescription()
));
}
}
?>
Solution 1:[1]
The problem was that is missing a "int" into abstract public function setDescription($value);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bruno Scorzafave de Almeida |
