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).
Related
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));
i'am not very well OO, and search a few web site to write this class
now my problem is how to use this class just like the Laravel 5.5 build in Storage class
i want use like this
MyStorage::disk('dropbox')
->addDirFilter('(school|travel)')
->addDirFilter('\d{4}-\d{2}-\d{2}')
->addFileFilter('.*\.jpg')
->getMatch();
here is MyStorage class
<?php
namespace App\MySupport;
class MyStorage
{
private $flag;
private $disk;
private $matches;
public function __construct()
{
$this->flag = false;
$this->disk = '';
$this->matches = [];
}
public function disk($disk)
{
$this->disk = $disk;
return $this;
}
public function addDirFilter($filter)
{
if (! $this->flag)
{
$this->flag = true;
$subDirs[] = \Storage::disk($this->disk)->directories();
}
else
{
foreach ($this->matches as $dir)
{
$subDirs[] = \Storage::disk($this->disk)->directories($dir);
}
}
$this->findMatch($subDirs, $filter);
return $this;
}
public function addFileFilter($filter)
{
if (! $this->flag)
{
$this->flag = true;
$subFiles[] = \Storage::disk($this->disk)->files();
}
else
{
foreach ($this->matches as $dir)
{
$subFiles[] = \Storage::disk($this->disk)->files($dir);
}
}
$this->findMatch($subFiles, $filter);
return $this;
}
public function findMatch($subItems, $filter)
{
// set empty before update
$this->matches = [];
// if call this method by addDirFilter() , $subItem contain the Dir path, eg, DirA/DirB
// if call this method by addFileFilter() , $subItem contain the File path, eg, DirX/File.txt
foreach (array_collapse($subItems) as $subItem)
{
// get the last str, eg, DirB OR File.txt
$lastStr = #end(explode('/', $subItem));
if ( preg_match('/^' . $filter . '$/u', $lastStr) )
{
// update new matches
$this->matches[] = $subItem;
}
}
}
public function getMatch()
{
return $this->matches;
}
}
Usage
<?php
namespace App\Http\Controllers;
use App\MySupport\MyStorage;
class TestController extends Controller
{
public function storageTest()
{
$MyStorage = new MyStorage();
// for example in my dropbox disk have
//
// school/2018-01-01/emails.txt
// school/2018-02-02/Peter.jpg
// travel/2017-06-06/TW.jpg
$folders = $MyStorage->disk('dropbox')
->addDirFilter('school')
->addDirFilter('\d{4}-\d{2}-\d{2}')
->getMatch();
// $folders result is:
// school/2018-01-01
// school/2018-02-02
$files = $MyStorage->disk('dropbox')
->addDirFilter('(school|travel)')
->addDirFilter('\d{4}-\d{2}-\d{2}')
->addFileFilter('.*\.jpg')
->getMatch();
// $files result is:
// school/2018-02-02/Peter.jpg
// travel/2017-06-06/TW.jpg
}
}
i tested seems all fine
can anyone point me to the direction, how to use MyStorage class just like Laravel 5.5 build in Storage class, thanks
You should use static function.
Try replace your function as code below.
public static function disk($disk)
{
$instance = new MyStorage();
$instance->disk = $disk;
return $instance;
Furthermore, I would suggest you learn singleton design pattern
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>";
}
I want to create a class that can remove duplicate items but uses pthreads I have an array with some duplicated lines and put them into a thread each line and here is my code.
header("Content-type: text/plain");
class Arr {
public $my_variable = array();
public function add($value) {
$this->my_variable[$value] = 1;
return $this->my_variable;
}
public function check($value)
{
if(isset($this->my_variable[$value])){
return true;
} else {
return false;
}
}
}
class workerThread extends Thread
{
public $arr;
public function __construct($i){
$this->i = str_replace("\r","",$i);
$this->i = str_replace("\n","",$this->i);
$this->arr = new Arr();
}
public function run()
{
if($this->arr->check($this->i)!==true)
{
$add = $this->arr->add($this->i);
echo date('H:i:s') . ' - '. $this->i . " - (".count($add).")\r\n";
}
}
}
$mailExp = array(
'talunays#gmail.com',
'talunays#gmail.com',
'talunays#gmail.com',
'talunays#gmail.com',
'talunays#gmail.com'
);
$total = count($mailExp);
for($i=0;$i<$total-1;$i++)
{
$workers[$i]=new workerThread($mailExp[$i]);
$workers[$i]->start();
}
But it doesn't work, duplicate lines still there and cannot be removed...
You should use construct only to set your private $this variables.
Make the logic inside the run function and use array_unique.
Let the workers come back with join() and retrieve the unduplicated arrays at this point.
I need to reconstruct head scripts and links so I would like to create a class that I can use for future references.
I started like :
class HeadClas{
public static $headprint;
function __construct(){
$this->headprint = "";
}
function addLinks(){
$this->headprint .= "addLinks";
return $this;
}
function addMeta(){
$this->headprint .= "addMeta";
return $this;
}
function printHead(){
return $this->headprint;
}
}
In the above case I would need to do
$print = new HeadClas;
$print->addLinks()->addMeta();
and I would like to do
$print = new HeadClas;
$print->printHead();
I did try
function printHead(){
return $this->addMeta()->addLinks();
}
But than I need to do
$print->printHead()->headprint;
What am I missing here?
Any help is appreciated!
Firstly, you should make the $headprint field non static. Making it static makes it the same reference for all future instances which will create undesirable results.
The printHead() should print the contents of that field like the method name suggests.
class HeadClass {
public $headprint;
function __construct() {
$this->headprint = "";
}
function addLinks() {
$this->headprint .= "addLinks";
return $this;
}
function addMeta() {
$this->headprint .= "addMeta";
return $this;
}
function printHead() {
$this->addLinks()->addMeta();
echo $this->headprint;
}
}
$printer = new HeadClass();
$printer->printHead();