pass variables from zf2 helper - php

I want to pass variables to some.phtml from zf2 view helper.
I want to return some data inside my view/application/some/some.phtml
public function __invoke($request)
{
}
can anyone suggest?

Why not just do:
public function __invoke($request)
{
$myVar = 'these';
$myOtherVar = 'probably';
$anotherVar = 'arent really strings';
return [$myVar, $myOtherVar, $anotherVar];
}
But I would probably try to refactor so this is all done in the controller. But if you state the actual problem you are having and what you are trying to achieve you will probably get a better answer

Related

How to access current context in php mustache as a function?

Right now I have something like this in my mustache template:
{{#render}}{{widget}}{{/render}}
And I have a viewModel which contains this code:
public function render() {
$View = $this->_View;
return function($widgetName, Mustache_LambdaHelper $helper) use (&$View) {
$widget = $helper->render($widgetName);
return $helper->render($View->mustache->getPartialsLoader()->load("$widget.view"));
};
}
I think you can see what I am trying to do here. I am trying to render a partial who's name is contained inside the widget-key of the current context.
Now the thing is, I really don't like the format I'm using in the template. I would prefer if I could write something like this:
{{renderWidget}}
then I would need to be able to access the current context directly in some way.
public function renderWidget() {
return function($context) {
return $helper->render($View->mustache->getPartialsLoader()->load("$context[widget].view"));
}
}
Can anybody tell me if that is possible in one way or another?

Modify Variable within Method with Service

I want to know the right way to handle this scenario: modify a variable within a method with a service. I know you could just do:
$input = $this->modify($input)
But that doesn't feel like the correct OOP way of handling the situation. Please see the inline comments.
class myClass {
public function __construct(Service $service)
{
$this->service = $service;
}
public function work($input)
{
// $input = SOMETHING
$this->service->modify($input);
// $input = SOMETHING MODIFIED
Entity::create($input);
}
}
It can be achieved by passing in the input as reference:
public function modify(&$input) {
// ^ See what I did there?
A better approach would be to return the modified input, and assign it again:
$input = $this->service->modify($input);
Don't start messing with references until you know exactly what you're doing.

Adding an array of objects to another array in codeigntier PHP?

im using codeigntier framework im trying to solve a problem to do with retrieving information from a database for example:
model.php:
public function read(){
$query = $this->db->get('table');
return $query->result();
}
controller.php:
public function doSomething () {
$exampleArray['name'] = "Bob's Database";
$getModel = $this->load->model('model','getData');
$modelData = $this->getData->read();
// i want to assign the the $modelData to a array element like so
$exampleArray['modelData'] = $modelData // here's where im stuck :(
}
thanks for your help!!
p.s. this is not an error, its just a question :)
}
If you want to be able to access $exampleArray outside of that method, you'll have to do one of two things:
a) set it as a class variable
class MyClass {
public $exampleArray;
.
.
.
}
then refer to it using
$this->exampleArray['index']
or b) pass it by reference into your doSomething() function:
public function doSomething(&$exampleArray) { ... }
The php manual has a section on variable scope that should help you better understand this.

Returning a static Class in PHP

I am working on a backend project. I need to return a static object withing another static object:
Class this_is_a_very_long_class_name
{
public static function call()
{
return self;
}
public static function script_link($link)
{
//doing stuff here...
}
}
Class Main
{
public static function view()
{
// trying to return View object
return this_is_a_very_long_class_name::call();
}
}
and I am trying to use it like this:
Main::view()::script_link('Some script');
So how can I accomplish that?
P.S.: I am not looking for another solution. I am looking for a answer what I asked.
You don't need that.
Use
View::script_link();
Also this is wrong and misleading view()->script_link because script_link is static
Addendum
If you your problem is your class name length I suggest you to create simple wrapper for this.
function createLink($string){
return VERY_LONG_CLASS_NAME_HELLO_PHP_NAMESPACE::script_link($string);
}
this way you just need to createLink();
in php 5.3: return new View(); (instead of return View::self;).
Manual: http://php.net/manual/en/language.oop5.basic.php#example-159
in php 5.2 use ReflectionClass
I think your syntax on the call is wrong. Since it is static, what you are trying to do would look something like this:
Main::view()::script_link('Some script');
Except that would give you a syntax error. Also, since it is static, you don't need to return anything. You should make it two separate calls:
Main::view();
View::script_link("Some script");
It makes no sense to say "I need to return a static object". If the class is defined, then the static object is present and can be accessed.
You just need a variable to hold the class, as a direct call is invalid syntax
Sample:
Class Main
{
public static function view($type)
{
// return some class
switch ($type) {
case "View 2":
return View2;
break;
default:
return View;
}
}
}
$v = Main::view("normal view");
$v::script_link('test');
Are you looking for functionality as late static binding? Which is supported from PHP 5.3. See here: http://php.net/manual/en/language.oop5.late-static-bindings.php

passing variables between functions in same controller using codeigniter

How to pass variable between two functions in same controller?
Do I need to use $this->session->set_flashdata is this right way to go ?
function load() {
$this->userhash = $this->uri->segment(3);
}
function save() {
$query_customer = $this->Customers->get_customer($this->userhash);
}
sorry if this is a basic stuff, but I am still learning.
I need to edit my question
In Ivan link, that I also googled out
there is a code which goes something like this
function index() {
$this->msg = 'data';
$this->testme();
}
function testme() {
echo $this->msg;
}
and its works, but I can not call $this->testme() directly in index(), so I can not load save() in load()
Passing data between two functions in my controller class

Categories