class call error

Asked

Viewed 89 times

-1

Good morning to all!! Guys, I’m going back to dealing with pure php and I came across a mistake half beast, but I’m not knowing how to solve...the error is :

C:\xampp\htdocs\PDO\index.php:5 Stack trace: #0 {main} thrown in 
C:\xampp\htdocs\PDO\index.php on line 5

I’ll post the 2 classes I call:

<?php 

namespace classes\NewConex;

class NewConex{

    protected $Conn;

    protected function Conex(){ $this->$Conn = new PDO('mysql:host=localhost;port=;dbname=pdo', 'root', '');  return $this; }

    protected function Dstroy(){ $this->$Conn=NULL; }

}

and the other is the crud:

<?php 
namespace classes\Crud;
use classes\NewConex;

class Crud{

     function __construct($x = array()) 
        { 
           $x = new NewConex; 
           $x->$Conn;
        }

    public function insert(){} 
}

and I’m calling these two in index.php as follows:

<?php 
require_once("classes/NewConex.php");
require_once("classes/crud.php");

$z = new Crud();

What’s wrong here guys? Thanks for your attention!!

  • 1

    I recommend adding the entire error message to the question, only with the part you have placed we have no information at all. In fact, it doesn’t make much sense to define all the fields of NewConex as protected. Maybe that’s the problem with your code.

1 answer

1

  1. Use of namespaces
  2. Class attribute encapsulation Visibility in PHP
  3. Use of class ownership - see
  4. Magical Methods - Destructors

Use of namespaces

In NewConex:

To use the class PDO (which is global PHP) within its namespace, in this case classes\NewConex, you need to insert a backslash '\' before the class name to indicate that this class (PDO) does not belong to the scope of its namespace, but to the global scope. That is to say, class is not inserted in its namespace.

Thus,

$this->Conn = new PDO('mysql:host=localhost;port=;dbname=pdo', 'root', '');  

becomes

$this->Conn = new \PDO('mysql:host=localhost;port=;dbname=pdo', 'root', '');  
# Repare na barra ^^ aqui.

In Crud:

The class NewConex belongs to the namespace classes\NewConex. The class Crud belongs to the namespace classes\Crud. Therefore, the two classes belong to different namespaces. And when you want to use a class that belongs to another namespace (e.g.: NewConex) in a namespace (ex: classes\Crud), you need to use

Instead of

use classes\NewConex;

typhoon,

use classes\NewConex\NewConex;

the "address" or name complete class NewConex is classes\NewConex\NewConex;.

Note: I believe that what you wanted was the two classes in a namespace called classes. Therefore, namespace names could not contain class names. They would therefore be, namespace classes;. And when I was gonna use them, I’d wear them like this use classes\NewConex and use classes\Crud. But if I’m wrong, disregard that remark.

In index.php

To use Crud, includes a reference to the class

use classes\Crud\Crud;

or refer it to your full name

$z = new classes\Crud\Crud();

Class attribute encapsulation

In Crud, you try to use the property $Conn of classes\NewConex\NewConex. However, this property has visibility protected, which means that it can only be accessed and manipulated by the very class that declares it (classes\NewConex\NewConex) and any classes it inherits (see inheritance of objects/classes if you have any questions).

Like classes\Crud\Crud does not inherit classes\NewConex\NewConex, she has no access to property Conn of NewConex.

Two options to fix this is to make Conn public

public $Conn;

instead of,

protected $Conn;

or create a method getter for Conn

protected $Conn;
//...

public function get_conn() 
{
    return $this->Conn;
}

Use of class ownership

I should have put this topic before the second.

  • In the declaration of a property you include $. Ex: public $Conn.
  • In use, you do not include $. Exs: $this->Conn; $NewConex->Conn.

Magical Methods - Destructors

I believe with the method classes\NewConex\NewConex\Dstroy(), you wanted to create a destructor magical method class classes\NewConex\NewConex. If that’s the case, the right thing to do would be to call him __destruct() and not Dstroy().

  • got it, problem solved, thank you very much!!!!!!

  • Glad you made it. Dispose. If you have more questions, post here for the community or I help you. If you think my answer has solved your problem, click on the "vezinho" that is in the left of my answer (see here how to accept an answer to help the community. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.