Home > Enterprise >  Unable to start class in php
Unable to start class in php

Time:11-13

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.

Infrastructure of the project

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()
        ));
    }
}

?>

CodePudding user response:

The problem was that is missing a "int" into abstract public function setDescription($value);

CodePudding user response:

In the class Book in the function setDescription you declare the of the passing Value with int. In Your astract Class you dont declare Type for Value. And php would throw an error. You can remove the type in the Book Class Method or add the type in the abstract class. But it's probably not meant to be int, is it? A string would be better.

Change public function setDescription(int $value) to: public function setDescription($value)

  • Related