Get data return php oop - php

getclass.php
public $set;
//dont need to care here the code is right , here is where it final result
default;
$this->actual_device = "desktop";
echo $this->issetValueNull($this->actual_device);
public function issetValueNull($mixed)
{
$this->set = $mixed;
}
getdata.php
require_once "getclass.php";
$check_detect_device = new detect_device();
if($check_detect_device->issetValueNull->set1 = "desktop"){
"<script>console.log('desktop');</script>";
}
i need to get the data from getclass.php to getdata.php and check the final result at getdata.php , some like below;
//this data return from getclass.php
if(isset($_GET['desktop'])){
"<script>console.log('desktop');</script>";
}
but i dont know how to return the data from getclass , can any one give me some advice ?

Im not sure is this what you are trying to do, but check my example:
<?php
class getClass
{
public $actualDevice;
public function __construct()
{
$this->actualDevice = "desktop";
}
public function setActualDevice($actualDevice)
{
$this->actualDevice = $actualDevice;
return $this;
}
public function getActualDevice()
{
return $this->actualDevice;
}
public function issetActualDevice()
{
return isset($this->actualDevice);
}
}
class getData
{
public $getClass;
public function __construct(getClass $getClass)
{
$this->getClass = $getClass;
}
public function checkDetectDevice($device)
{
if ($this->getClass->getActualDevice() == $device) {
return true;
}
return false;
}
}
$getClass = new getClass();
$getData = new getData($getClass);
if ($getData->checkDetectDevice($_GET['desktop'])) {
echo "<script>console.log('desktop');</script>";
}

Related

PHP - Json is empty after converting object

So basically I want to have an Object Class with a bunch of setters that I can manipulate and then convert straight to JSON. In the past I was using an already existing JSON as model(json->object->json). Now I want to just have object->json.
But at the moment I get an empty array when I try to use json_encode.
Also had some trouble with my $index that I'm using because I can have multiple numerical indexes inside "items"
PHP:
<?php
namespace ProjectApi\Models;
use ArrayObject;
use stdClass;
class MagentoInvoice
{
public $capture = true;
public $notify = true;
public function __construct()
{
}
public function setCapture($capture)
{
$this->capture = $capture;
}
public function getCapture()
{
return $this->capture;
}
public function setNotify($notify)
{
$this->notify = $notify;
}
public function getNotify()
{
return $this->notify;
}
public function setItems($itemsCount)
{
$i = 0;
while($i < $itemsCount)
{
$this->items = new stdClass();
$i++;
}
}
public function getItems()
{
return $this->items;
}
public function setProductQty($index, $qty)
{
$this->items->$index->qty = $qty;
}
public function getProductQty($index)
{
return $this->items->$index->qty;
}
public function setProductOrderItemId($index, $id)
{
$this->items->$index->order_item_id = $id;
}
public function getProductOrderItemId($index)
{
return $this->items->$index->order_item_id;
}
}
JSON
{"capture":true,"items":[{"order_item_id":123,"qty":1},{"order_item_id":321,"qty":1}],"notify":true}
$body = new MagentoInvoice();
$body->setCapture(true);
$body->setNotify(true);
if($firstProductId != null && $secondProductId != null)
{
$body->setItems(2);
$body->setProductQty(0, 1);
$body->setProductOrderItemId(0, $firstProductId);
$body->setProductQty(1, 1);
$body->setProductOrderItemId(1, $secondProductId);
}
print_r(json_encode($body));

Not getting desired output from PHP class

This is my very first question actually. I am under a learning process of OOP PHP and trying to design my first class for Category. Here is my code
<?php
class category{
public $catid;
public $datacol;
public function __construct($catid=0)
{
$this->catid=0;
$this->datacol=$this->load($catid);
}
public function load($catid)
{
global $conn;
$sql=' select * '
.' FROM tbl_categories'
.' WHERE catid='.$catid;
if (!($res=mysqli_query($conn,$sql)) || !mysqli_num_rows($res)>0)
return false;
$this->datacol = mysqli_fetch_array($res);
$this->catid = $this->datacol['catid'];
return true;
}
public function reload() {
return $this->load($this->getId());
}
/* ------------------> Getter methods <--------------------- */
public function getId() { return $this->catid; }
public function getName() { return $this->datacol['category']; }
public function getOneliner() { return $this->datacol['categoryoneliner']; }
public function getBrief() { return $this->datacol['categorybrief']; }
public function getThumb() { return $this->datacol['timg']; }
public function getImage() { return $this->datacol['limg']; }
public function getParentID() { return $this->datacol['parentcat']; }
public function getPagetitle() { return $this->datacol['catpagetitle']; }
public function getKeywords() { return $this->datacol['catkeywords']; }
public function getMetadesc() { return $this->datacol['categorymetadesc']; }
public function getPriority() { return $this->datacol['priority']; }
public function getRemarks() { return ($this->datacol['remarks']); }
public function getRow() { return $this->datacol; }
}
?>
Now,
When i create its object from other file with a code as below:
$cat=new category(10);
echo var_dump($cat);
echo var_dump($cat->getId());
echo var_dump($cat->getName());
The output gives me the correct catid when called getID(). But Shows NULL for getName() or any other function other than getID().
What am i doing wrong?
Thanks in advance
As #Arnold Gandarillas said, you are overwriting $datacol in the constructor.
Your constructor calls load(), which sets $datacol:
public function load($catid)
{
...
$this->datacol = mysqli_fetch_array($res);
return true;
}
The function then returns true. In the constructor, this true is assigned to $datacol, overwriting the previous value:
public function __construct($catid=0)
{
$this->catid=0;
$this->datacol=$this->load($catid);
}
Change the constructor to something like this:
public function __construct($catid=0)
{
if ($catid > 0)
$this->load($catid);
}
Alright - look closely to your function:
public function load($catid)
{
global $conn;
$sql=' select * '
.' FROM tbl_categories'
.' WHERE catid='.$catid;
if (!($res=mysqli_query($conn,$sql)) || !mysqli_num_rows($res)>0)
return false;
$this->datacol = mysqli_fetch_array($res);
$this->catid = $this->datacol['catid'];
return true;
}
So, you function returns either true of false. And this boolean values is assgined to $this->datacol here:
$this->datacol = $this->load($catid);
As this assignment happens after you did $this->datacol = mysqli_fetch_array($res);, your $this->datacol becomes either true or false, and holds no real data from mysql.
So, one of the solutions can be to remove assignment:
public function __construct($catid=0)
{
$this->catid=0;
$this->load($catid);
}
In this case your $this->datacol is not overwritten.
its simple $this->datacol = $this->load($catid); is not required
you have assigning $this->datacol twice
public function __construct($catid=0)
{
$this->catid=0;
$this->load($catid);
}
public function load($catid)
{
global $conn;
$sql=' select * '
.' FROM tbl_categories'
.' WHERE catid='.$catid;
if (!($res=mysqli_query($conn,$sql)) || !mysqli_num_rows($res)>0)
return false;
$this->datacol = mysqli_fetch_array($res);
$this->catid = $this->datacol['catid'];
return true;
}
please add if anything is missing

PHP Optional Function Parameter Set to Reference to Existing Object

I don't even know if this is possible but I'm trying to set an optional value to an existing object.
Here is a simplified version of the code I'm trying.
<?php
class configObject {
private $dataContainer = array();
public function set($dataKey, $dataValue) {
$this->dataContainer[$dataKey] = $dataValue;
return TRUE;
}
public function get($dataKey) {
return $this->dataContainer($dataKey);
}
$this->set('someValue', 'foobar');
} //End configObject Class
function getPaginationHTML($c = &$_config) {
$someOption = $c->get('someValue');
// Do other stuff
return $html;
}
$_config = new configObject();
$html = getPaginationHTML();
?>
I'm getting the error:
syntax error, unexpected '&' in
Any help is appreciated, again I'm not sure if it's even possible to do what I'm trying to do so sorry for being a noob.
Thanks
example with the decorator pattern:
class ConfigObject {
private $dataContainer = array();
public function set($dataKey, $dataValue) {
$this->dataContainer[$dataKey] = $dataValue;
return true;
}
public function get($dataKey) {
return $this->dataContainer[$dataKey];
}
}
class ConfigObjectDecorator {
private $_decorated;
public function __construct($pDecorated) {
$this->_decorated = $pDecorated;
}
public function getPaginationHTML($dataKey) {
$someOption = $this->get($dataKey);
// Do other stuff
$html = '<p>' . $someOption . '</p>';
return $html;
}
public function set($dataKey, $dataValue) {
return $this->_decorated->set($dataKey, $dataValue);
}
public function get($dataKey) {
return $this->_decorated->get($dataKey);
}
}
class ConfigFactory {
public static function create () {
$config = new ConfigObject();
return new ConfigObjectDecorator($config);
}
}
$config = ConfigFactory::create();
if ($config->set('mykey', 'myvalue'))
echo $config->getPaginationHTML('mykey');
Note that can easily rewrite ConfigFactory::create() to add a parameter to deals with other types of decoration (or none).

Codeigniter comparison input with database?

I try to comparise string from input to string in database.
This is my code:
Controller:
public function __construct() {
parent::__construct();
$this->captcha_rejestracja = rand(1,40);
}
public function register() {
$str = $this->input->post('captcharejestr');
$this->form_validation->set_rules('captcharejestr', 'kod z obrazka', 'trim|required|alpha_numeric|xss_clean|callback_captcha_check[$str]');
}
public function captcha_check($str) {
$jakacaptcha = $this->captcha_rejestracja;
$this->load->model('Tresci');
if ($str != $this->Tresci->captcha($jakacaptcha))
{
$this->form_validation->set_message('captcha_check', 'Błędnie przepisany kod z obrazka.');
return FALSE;
}
else
{
return TRUE;
}
}
Model:
public function captcha($jakacaptcha) {
$query = $this->db->query('SELECT kod FROM captcha WHERE numer='.$jakacaptcha.'');
$row = $query->row_array();
return $row['kod'];
}
I dont know where is the problem because everytime i have return FALSE from my callback ??
You don't seem to be running the form validation.
public function __construct()
{
parent::__construct();
// this is somewhat confusing, why randon value ?
// the database query should have one value :-/
$this->captcha_rejestracja = rand(1,40);
$this->load->model('Tresci');
}
public function register()
{
$this->form_validation->set_rules('field', 'label', 'callback_captcha_check' );
if(!$this->form_validation->run()){
//show the form again
return;
}
//create a new user
}
public function captcha_check($str)
{
$this->form_validation->set_message('captcha_check', 'Message here...');
$result = $this->tresci->captcha($this->captcha_rejestracja);
return ( $str == (string)$result ) ? true : false;
}

PHP method chaining

Can anyone explain me why this code does not work (the content $this->_string) is empty?
<?php
class WordProcessor
{
public $_string = '';
public function __constructor($text)
{
$this->_string = $text;
}
public function toLowerCase()
{
$this->_string = strtolower($this->_string);
return $this;
}
public function trimString()
{
echo $this->_string;
$this->_string = trim($this->_string);
return $this;
}
public function capitalizeFirstLetter()
{
$this->_string = trim($this->_string);
return $this;
}
public function printResult()
{
echo $this->_string;
}
}
$data = new WordProcessor("here Are some words! ");
$data->trimString()->toLowerCase()->capitalizeFirstLetter()->printResult();
Use construct instead of constructor?
Its
public function __construct($text)
not __constructor(..)

Categories