My array list does not store or even retrieve items back, when the user press rent the it get the value of the item (the id item) and then it store it on the arryList, and from the basket file it should retrieve the selected item by the user from the database using the item id. the main problem i have is that adding and retrieving seems like not working
class Shop {
static $_item = array();
public function __construct(){
}
public function addItem($id)
{ self::$_item[] = $id;
}
public function getId()
{
foreach(self::$_item->s as $s)
{
return $s;
}
}
}
<?php
require_once('Models/Dvd_sql.php');
require_once('Models/Shop.php');
$view = new stdClass();
$view->dd = 'SQL';
$dvd_sql = new Dvd_sql();
$view->dd = $dvd_sql->fetchAllStudents(); //->fetchAllStudents();
if(isset($_POST['rent']))
{
$shop = new Shop();
$shop->addItem($_POST['trying']);
}
require_once('Views/dvdDetails.phtml');
<?php
require_once('Models/Basket.php');
require_once('Models/Shop.php');
$view = new stdClass();
$view->login = 'Homepage';
$view->dd = 'SQL';
$shop = new Shop();
$basket = new Basket();
$d = $shop->getId();
$view->dd = $basket->getFrom($d);
One problem I see is that in the following foreach, it will exit the function on the return and return only the first item. Plus since it is an array, it does not have the 's' property
Since you have multiple items in $shop, you can retrieve the complete array and then process them from outside the class
public function getId()
{
return self::$_item;
}
outside the class
$shop = new Shop();
$basket = new Basket();
$d = $shop->getId();
foreach ($d as $id){
$view->dd = $basket->getFrom($id);
// do whatever you wish to do with the retrieved basket item
// it's not clear what you wish to do with the item.
}
Related
I'm trying to override my constructor to create an object based on a row in my database and I'm unsure of the best way to go about it. My goal is to be able to create the object given the constructor but also have the ability to create the object from a row in my database.
Here is the pseudocode for my given scenerio:
class PurchaseOrder
{
private $id;
private $title;
private $amount;
function __construct($title, $amount)
{
$this->title = $title;
$this->amount = $amount;
}
function constructFromDB($pdo, $id)
{
$poValues = queryPO($pdo, $id); // fetches from db (psuedocode);
$this->title = $poValues->title;
$this->amount = $poValues->amount;
}
}
// Create new purchase order
$po = new PurchaseOrder("Purchase Order 1", 100);
// Query purchase order from database
$queriedPO = new PurchaseOrder()->constructFromDB($conn, 1);
But as you know, I cannot leave the constructor empty for my object to be initialized. I cannot have it be a static function as I will need to be able to manipulate the object's properties.
The constructFromDB() method can be static and return an object:
public static function constructFromDB($pdo, $id)
{
$poValues = queryPO($pdo, $id); // fetches from db (psuedocode);
$title = $poValues->title;
$amount = $poValues->amount;
$class = __CLASS__;
return new $class($title, $amount);
}
Then call it statically:
$queriedPO = PurchaseOrder::constructFromDB($conn, 1);
Lets say, we have the following PHP class:
class Product {
protected $data = array();
protected $modified = false;
public function __construct($data) {
$this->data = $data;
}
public function & __get($name) {
if(array_key_exists($name, $this->data)) {
return $this->data->$name;
}
$null = null;
return $null;
}
public function __set($name, $value) {
$this->data->$name = $value;
$this->modified = true;
}
}
$obj = new Product([]);
If I now set a value (for example $obj->name = "Name";), the class property $modified is set to true. Thats what I want to achive.
But is it somehow possible, to "track" modifications, if the are done in object values? For example:
$property = new stdClass();
$property->name = "Name of Prop";
$obj = new Product([
"name" => "Name",
"someObject" => $property
]);
// Now here comes the change
$obj->someObject->name = "New Name";
Because with the code above, $obj->modified would be false
I guess, you have to pass the new value to the "set" function. The way you do it just changes the value itself without going into your function where you declared, that "modified" turns true.
Like this maybe?
$obj->someObject->__set($name, 'New Name');
I have a simple array thats returned when executing:
$this->forecast($value->id,$value->db_connection)
when I call the $reports::create($data) it duplicates each row, i have added some logic to print out $data within the foreach and it never returns duplicates, when printing the $result object it has 2 entries.
any idea where i am going wrong?
public function store() // function to store report data in db
{
$reports = new Reports;
$companyData = new ClientSettings;
$settings = $companyData::all();
foreach ($settings as $value) {
$data = $this->forecast($value->id,$value->db_connection);
$result = $reports::create($data);
}
echo "Forecast generated";
}
public function store() // function to store report data in db
{
$reports = new Reports;
$companyData = new ClientSettings;
$settings = $companyData::all();
foreach ($settings as $value) {
$data = $this->forecast($value->id,$value->db_connection);
$result = Reports::create($data);
}
echo "Forecast generated";
}
I'm new in PHP-POO. I would like to retrive objects from an array and access to that object properties.
My intent code is:
require_once("../modelo/ClubDAO.php");
require_once("../modelo/Club.php");
require_once("../utils/ArrayList.php");
$clubs = new ArrayList();
/* GET ALL THE CLUBS OF THE DATABASE (WORKS GOOD)*/
$clubs = ClubDAO::get_instancia()->getAllClubs();
for($i = 0; $i < $clubs->size(); $i++)
{
$club = new Club();
$club->getNif(); /* HERE I CAN ACCESS TO THE FIELDS */
$club = $clubs->item($i);
$club->/*HERE I CAN'T ACCES TO THE FIELDS OF THE CLASS*/
}
ArrayList class is an encapsulation of a simple array, for me it's easier to work with, here the code:
class ArrayList {
var $array;
public function ArrayList() {
$this->array = array();
}
public function addItem($item){
$this->array[] = $item ;
}
public function toString(){
$cadena = "";
foreach ($this->array as $item) {
$cadena .= $item;
}
return $cadena;
}
public function delete($item){
unset($this->array[$item]);
}
public function item($item){
return $this->array[$item];
}
public function size(){
return count($this->array);
}
}
You are overwriting your variable:
$club = $clubs->item($i);
So it will not be the object you expect any more.
I'm very new with PHP and OOP techniques.
So I have no problem creating a object like so:
$member1 = new Member('person 1');
But is there a way to return a bunch of objects. So that I can iterate through them and put them in the DOM?
class Example
{
public function getPeople()
{
$objs = array();
for($i = 0; $i < 10; $i++)
{
$obj[] = new Person('Person ' . ($i + 1));
}
return $objs; // returning an array of Person objects
}
}
$example = new Example();
foreach($example->getPeople() as $person)
{
// Do something with $person
}
In order to get objects, one possible way is to use array :-
$members = new members( array(...));
// you can assess any member via $members->members[$some_id]
// class to return list of members
class members ()
{
public $members = array();
public function __construct( array $ids)
{
foreach ($id as $id)
{
$this->members[$id] = new Member($id);
}
}
}
class member()
{
public function __construct($id)
{
// any other thing you want to do
}
}