Reputation: 119
i try to illustrate articles about PHP and have built with this structure:
Article
-- ChildArticle
I now want to access a function in the ChildArticle class that is inherited from the Article class.
Here is my Article class:
<?php
namespace App\Article;
use PDO;
class Articles
{
private $id;
private $name;
private $cid;
private $ordernumber;
private $description;
private $descriptionLong;
private $childArticles;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @return mixed
*/
public function getCid()
{
return $this->cid;
}
/**
* @return mixed
*/
public function getOrdernumber()
{
return $this->ordernumber;
}
/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}
/**
* @return mixed
*/
public function getDescriptionLong()
{
return $this->descriptionLong;
}
/**
* @return mixed
*/
public function getChildArticles()
{
return $this->childArticles;
}
function readChildArticles(PDO $pdo){
$stmt = $pdo->prepare(
"SELECT articleID as id, ordernumber FROM `s_articles_details` WHERE ordernumber LIKE :ordernumberWOD AND ordernumber NOT LIKE :ordernumber"
);
$stmt->execute([
'ordernumberWOD'=>$this->ordernumber.".%",
'ordernumber'=>$this->ordernumber
]);
$this->childArticles = $stmt->fetchAll(PDO::FETCH_CLASS,"App\\Article\\ChildArticle");
}
}
Here is my ChildArticle class:
class ChildArticle extends Articles
{
}
Now I want to read the order numbers of the child articles:
foreach ($article->getChildArticles() as $child){
echo "Child: {$child->getOrdernumber()}<br>";
}
My readChildArticles function:
function readChildArticles(PDO $pdo){
$stmt = $pdo->prepare(
"SELECT articleID as id, ordernumber FROM `s_articles_details` WHERE ordernumber LIKE :ordernumberWOD AND ordernumber NOT LIKE :ordernumber"
);
$stmt->execute([
'ordernumberWOD'=>$this->ordernumber.".%",
'ordernumber'=>$this->ordernumber
]);
$this->childArticles = $stmt->fetchAll(PDO::FETCH_CLASS,"App\\Article\\ChildArticle");
}
My output is like:
Child:
Child:
and so on
if i get the ordernumber via $child->ordernumber, it works.
Why doesn't my ChildArticle class take over the function exactly like the parameters?
And can you give me a hint how I can solve the problem or where I can read about it?
thx :)
Upvotes: 0
Views: 46
Reputation: 522015
The problem is in the way PDO constructs the object: it creates an instance of the class (ChildArticle
), then sets all the columns as properties on it. Since $ordernumber
is private
, this creates a separate property in each class, i.e. Article::$ordernumber
and ChildArticle::$ordernumber
are two entirely separate properties. The method in Article
tries to access Article::$ordernumber
.
Make the property at least protected
, or change the way in which you instantiate the class.
If you don't have a good reason for using separate properties and getters, you may as well consider making the property public
and getting rid of the getter method.
Upvotes: 1