Can not call many function : Zend_View_Helper - php

Can not call many function: Zend_View_Helper
helpers : MainHelpers.php
Class Zend_View_Helper_MainHelpers {
public function mainHelpers(){
$output="ok 1";
return $output;
}
public function mainHelpers2(){
$output="ok 2";
return $output;
}
}
view : detail.phtml
<?php echo $this->mainHelpers(); ?> // ok call function
<?php echo $this->mainHelpers2(); ?> // not ok call function
I want to call many function in zend_view_helper.

If you want your view helper to contain additional methods besides its constructor, make sure you return the object instance and do something like this:
Class Zend_View_Helper_MainHelpers {
public function mainhelpers() {
return $this;
}
public function foo(){
$output="ok 1";
return $output;
}
public function bar(){
$output="ok 2";
return $output;
}
}
Now call your helper methods like this:
$this->mainhelpers()->foo()
$this->mainhelpers()->bar()
Judging from your code example, it seems you are trying to encapsulate more than one view helper inside one class. You are probably better of by creating more view helpers:
class Zend_View_Helper_Foo()
{
public function foo()
{
// do stuff
}
}
class Zend_View_Helper_Bar()
{
public function bar()
{
// do more stuff
}
}

Related

Why use a return when calling parent?

I am learning OO PHP and I was experimenting with using a parent::method in a child class. I noticed i had to use an "extra" return for the output of the parent method to show up. Could someone explain me why this is?
This is the code I used and in the code I made a comment.
class ShopProduct {
public $productnumber;
public function __construct($productnumber) {
$this->productnumber = $productnumber;
}
public function getSummary(){
return $this->productnumber;
}
}
class BookProduct extends ShopProduct {
public function __construct($productnumber) {
parent::__construct($productnumber);
}
public function getSummary() {
return parent::getSummary(); // if i dont use return it doesnt work? why is that?
// parent::getSummary(); is not enough it seems.
}
}
$product = new BookProduct(11111);
echo $product->getSummary();
?>
public function getSummary() {
return parent::getSummary(); // if i dont use return it doesnt work? why is that?
// parent::getSummary(); is not enough it seems.
}
Replace parent::getSummary() with any other function or method call:
public function getSummary() {
foo();
}
Of course you wouldn't expect getSummary to return anything in this case, right? Just because the method you're calling is parent::... doesn't change anything about this behaviour. It does not return automagically, because you may want to do something like this:
public function getSummary() {
$summary = parent::getSummary();
return "Book: $summary";
}
BTW, if the only thing your method does is call its parent, you can leave out the entire method. In other words, this:
class BookProduct extends ShopProduct {
public function __construct($productnumber) {
parent::__construct($productnumber);
}
public function getSummary() {
return parent::getSummary();
}
}
is exactly the same as this:
class BookProduct extends ShopProduct { }

How to call the same class function in controller

My sample code.. i dont know how to call
class sample
{
public function actionExample()
{
code here
// To call php function
}
public function a()
{
echo "test":
}
}
class className {
function mone() {
// CODE
}
function mTwo() {
// calling mone in mtwo
$this->mone();
}
}
bu using $this
eg:-
$this->functionName();
$this acts as an object of the containing controller and using this you can access the variables and functions as well.
eg:-
public $myvar;
You can access it using
$this->myvar='value';
similarly for functions
public function firstfun()
{
//some code
}
you can call it using
$this->firstfun()
TRy
class sample
{
public function actionExample()
{
$this->a();
// To call php function
}
public function a()
{
echo "test":
}
}
Check this for more reference.
http://www.yiiframework.com/forum/index.php/topic/6471-call-another-controllers-action/

Overriding static methods in PHP

I have an abstract page class looking like this:
abstract class Page {
public static function display() {
self::displayHeader();
self::displayContent();
self::displayFooter();
}
public static function displayContent() {
print "<p>some content</p>";
}
public static function displayHeader() {
include_once(kContent . "HeaderContent.class.php");
HeaderContent::display();
}
public static function displayFooter() {
include_once(kContent . "FooterContent.class.php");
FooterContent::display();
}
};
I would like to subclass from this, and only override the displayContent method, so the header and footer is being displayed automatically, but still having the option to override the display method, for example for .js files.
Now I have another class, looking like this:
class FooPage extends Page {
public static function displayContent() {
print "<p>Foo page</p>";
};
Now, instead of calling the FooPage's displayContent method, it just calls the one from the superclass.
Why? What can I do?
EDIT
I'm running PHP 5.2.17
Ilija, PHP < 5.3 doesn't have "Late Static Binding" and that's why you may be experiencing the FooPage::displayContent not being called. If you are running PHP 5.2 then there is nothing much to do (except for some hacks using debug_backtrace(), which honestly I wouldn't recommend for this situation).
Now, what it really calls my attention is that your methods are all static; is there a reason for this? Why aren't they instance methods? I would expect something like:
include_once(kContent . "HeaderContent.class.php");
include_once(kContent . "HeaderContent.class.php");
abstract class Page
{
protected $header;
protected $footer;
public function __construct()
{
$this->header = new HeaderContent();
$this->footer = new FooterContent();
}
public function display()
{
$this->displayHeader();
$this->displayContent();
$this->displayFooter();
}
public function displayContent()
{
print "<p>some content</p>";
}
public function displayHeader()
{
$this->header->display();
}
public function displayFooter()
{
$this->footer->display();
}
};
class FooPage extends Page
{
public function displayContent()
{
print "<p>Foo page</p>";
}
}
and later in your view you would write something like:
$page = new FooPage();
$page->display();
Some things to take into account:
It is generally better not to use print/echo when generating a view content. Instead try to create the string and do the print/echo as a last step. This makes it easier to later write tests.
Example:
public function display()
{
return
$this->displayHeader() .
$this->displayContent() .
$this->displayFooter();
}
public function displayContent()
{
return "<p>some content</p>";
}
public function displayHeader()
{
return $this->header->display();
}
....
$page = new FooPage();
echo $page->display();
If you need to do it as your application grows, you can pass the header and footer as Page constructor parameters. As long as they are objects that understand the display() message (i.e. polymorphic) things should be ok.
HTH
Returned back to this question. Was looking for solution for Symfony (5.4).
And I finally came with this "Service - method call" solution.
#services_dev.yaml:
Company\Core\PinGenerator\PinGenerator:
calls:
- [setDebugMode, [true]]
#PinGenerator:
class PinGenerator implements PinGeneratorInterface
{
public static bool $inDebugMode = false;
public static function setDebugMode(bool $inDebugMode): void
{
self::$inDebugMode = $inDebugMode;
}
public static function generate(int $length = self::DEFAULT_PIN_CODE_LENGTH, bool $numbersOnly = true): string
{
if (!self::$inDebugMode) {
return PinGeneratorProd::generate($length, $numbersOnly);
} else {
return PinGeneratorDev::generate($length, $numbersOnly);
}
}
}
Honesly hoping, that this will help someone, someday.

PHP OOP Multicall

i have seen in some libraries something like this :
$this->getResponse()->setRedirect($returnUrl);
How is this 'multicall' done, or, how should the class be build to do something like this?
I think :
class greeting
{
public function hi()
{
public function howAreYou()
{
echo 'How are you?';
}
}
}
$greet = new greeting;
$greet->hi()->howAreYou();
But i think it's not so good, i would better use something like extends, but i don't know. Thx for your suggestions.
If this is a class instance calling itself, it is called "method chaining".
In PHP, can be done by using return $this; note that this is a very different mechanism than class inheritance - it doesn't really make sense to treat them as interchangeable.
See also: https://stackoverflow.com/search?q=method+chaining+php
getResponse() is returning a class instance which has a setRedirect() method.
Example:
class Foo
{
public function getResponse()
{
$redirect = new Bar();
return $redirect;
}
}
class Bar
{
public function setRedirect($returnUrl)
{
// do something
}
}
$foo = new Foo();
$foo->getResponse()->setRedirect("returnUrl");
No.
All you have to do is return self at very end of each function.
So Your example would be like>
class greeting
{
public function hi()
{
echo "Hi";
return $this;
}
public function howAreYou()
{
echo 'How are you?';
return $this;
}
}
$greet = new greeting;
$greet->hi()->howAreYou();
Or even:
$greet->hi()->howAreYou()->hi()->howAreYou();
class stutter{
public function a(){
echo 'h';
return $this;
}
public function b(){
echo 'hello world!';
}
}
$var=new stutter();
var->a()->b();
Output is:
h hello world
Chaining methods is not the same as declaring functions within a method... in fact the latter will spit an error (not the function declaration, but the way you're calling it). In order to chain a method, just have it return the object itself:
Class chainableObject
{
public $name=null;
public function __construct($name='')
{
$this->name=$name;
return $this;
}
public function setName($name)
{
$this->name = $name;
return $this;//makes chainable
}
public function greet()
{
echo 'Hello, '.$this->name;
return $this;
}
}
$chain = new chainableObject('Frank')->greet();//outputs: Hello, frank
The explanation: All methods return the instance itself, so basically, read the last line of the snippet like this [create object with name:Frank]=>call method greet on the return value of this action. Since the return value is $this, the object that has a greet method, that's what will happen... easy, for more info: just google php method chaining

Calling a function within a Class method?

I have been trying to figure out how to go about doing this but I am not quite sure how.
Here is an example of what I am trying to do:
class test {
public newTest(){
function bigTest(){
//Big Test Here
}
function smallTest(){
//Small Test Here
}
}
public scoreTest(){
//Scoring code here;
}
}
Here is the part I am having problems with, how do I call bigTest()?
Try this one:
class test {
public function newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public function scoreTest(){
//Scoring code here;
}
}
$testObject = new test();
$testObject->newTest();
$testObject->scoreTest();
The sample you provided is not valid PHP and has a few issues:
public scoreTest() {
...
}
is not a proper function declaration -- you need to declare functions with the 'function' keyword.
The syntax should rather be:
public function scoreTest() {
...
}
Second, wrapping the bigTest() and smallTest() functions in public function() {} does not make them private — you should use the private keyword on both of these individually:
class test () {
public function newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public function scoreTest(){
//Scoring code here;
}
}
Also, it is convention to capitalize class names in class declarations ('Test').
Hope that helps.
class test {
public newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public scoreTest(){
//Scoring code here;
}
}
I think you are searching for something like this one.
class test {
private $str = NULL;
public function newTest(){
$this->str .= 'function "newTest" called, ';
return $this;
}
public function bigTest(){
return $this->str . ' function "bigTest" called,';
}
public function smallTest(){
return $this->str . ' function "smallTest" called,';
}
public function scoreTest(){
return $this->str . ' function "scoreTest" called,';
}
}
$test = new test;
echo $test->newTest()->bigTest();
To call any method of an object instantiated from a class (with statement new), you need to "point" to it. From the outside you just use the resource created by the new statement.
Inside any object PHP created by new, saves the same resource into the $this variable.
So, inside a class you MUST point to the method by $this.
In your class, to call smallTest from inside the class, you must tell PHP which of all the objects created by the new statement you want to execute, just write:
$this->smallTest();
In order to have a "function within a function", if I understand what you're asking, you need PHP 5.3, where you can take advantage of the new Closure feature.
So you could have:
public function newTest() {
$bigTest = function() {
//Big Test Here
}
}
class sampleClass
{
public function f1()
{
return "f1 run";
}
public function f2()
{
echo ("f2 run" );
$result = $this->f1();
echo ($result);
}
f2();
}
output :
f2 run
f1 run
You need to call newTest to make the functions declared inside that method “visible” (see Functions within functions). But that are then just normal functions and no methods.
example 1
class TestClass{
public function __call($name,$arg){
call_user_func($name,$arg);
}
}
class test {
public function newTest(){
function bigTest(){
echo 'Big Test Here';
}
function smallTest(){
echo 'Small Test Here';
}
$obj=new TestClass;
return $obj;
}
}
$rentry=new test;
$rentry->newTest()->bigTest();
example2
class test {
public function newTest($method_name){
function bigTest(){
echo 'Big Test Here';
}
function smallTest(){
echo 'Small Test Here';
}
if(function_exists( $method_name)){
call_user_func($method_name);
}
else{
echo 'method not exists';
}
}
}
$obj=new test;
$obj->newTest('bigTest')
You can also use self::CONST instead of $this->CONST if you want to call a static variable or function of the current class.

Categories