- Use of namespaces
- Class attribute encapsulation Visibility in PHP
- Use of class ownership - see
- 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()
.
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
asprotected
. Maybe that’s the problem with your code.– Woss