2

Here is my question I have a class say ABC and a function XYZ defined inside it and that function is returning something based on the above logic in it.

class ABC {
function XYZ{
..........
.......
return "--- ";
}
}

Now there is an object on another page of this class which calls this method.

$z= new ABC;

if( $z->XYZ() )
{
some output
}
else{
another output
}

My problem is i dont have access to PAGE 2. but i need to change the output of else statement. I have only access to class ABC because i am overriding it in a module. so, in short i can only alter function XYZ. Whats the solution??

If its of any significance, i am working on MAGENTO ECOMMERCE Platform and class is a block class and page 2 is a template

sunny
  • 1,156
  • 8
  • 15
  • 3
    Looks like there's no solution except editing page2. – u_mulder Sep 26 '14 at 18:55
  • @u_mulder yes. but i cant because i have to make an extension, and every magento have different templates. I can only override core classes but cannot overide the theme's template even if i want. – sunny Sep 26 '14 at 19:03
  • Even the template pages can be overriden, but i cannot do that in this case. I want the users page to work as it is, except that else part – sunny Sep 26 '14 at 19:08
  • `function XYZ{ if ( $this->originalXYZ() ) { some output } else { modified another output } exit(); }` – guest Sep 29 '14 at 19:35
  • @guest how can i write modified another output without touching the template? – sunny Sep 30 '14 at 06:19
  • @sunny All you can do at this point is to make sure the function returns true and doesn't reach the "else". You can't modify "another" output because you have a bool value to check : it reaches that else statement or not. – despina Sep 30 '14 at 13:34
  • The point was that you subsume the branching and output functionality into `ABC::XYZ` (where it's under your control). Then, you `exit` at the end of `ABC::XYZ` so that the original untouchable template code won't run. – guest Sep 30 '14 at 20:23
  • Not enough info. What is "page2" returning? If it a numerical value you can use a switch statement, otherwise you will likely need to use reqex to break the returned string into something that can be handled based on its content. This can be solved with more info on whet your tying to achieve, and a solid example of what your working with (such as a sample of what is being returned) – JSON Oct 04 '14 at 09:38
  • @JSON how can i use switch statement if can not touch page 2. Please read above comments. And moreover page 2 is default product view page of magento. Do you want me to write the complete code of that template?? – sunny Oct 04 '14 at 14:26
  • It would be an awful security issue, if that would be possible what you want. Fortunately this is absolutely impossible! – Peter Oct 04 '14 at 20:19

4 Answers4

3

You can alter the output html with an observer with the http_response_send_before event.

Capture the html and do your stuff.

You have a good explanation here

I hope this can help you

Community
  • 1
  • 1
apouey
  • 310
  • 1
  • 7
1


you can also do as follow :

<?php
ob_start();
// do your stuff
// ...

// here you capture all the output that it is generated by your scripts
$v = ob_get_contents();

// alter the value of $v by detecting if it should be changed. you can user regex for example to 
// detect and update
// ...
// here you clean all data stored in buffer
ob_clean();
// and put the updated data in buffer
print $v;

// end of buffer operations
ob_end_flush();

?>

hope it helps,

Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
0

You need a way to differentiate PAGE1 from PAGE2, lets say by the URL if it's possible. So you would have to change your method that will check on which page is currently and according to it, it will change the output, but you must write the output in the method itself instead of writing it in the template. class ABC { function XYZ { $result = $this->getResult(); //Helper method to get the result. if ($result) { //Do something when result is true. } else { $url = $this->checkUrl(); //Check URL of the page. if ($url == PAGE2) { //If you are on PAGE2 //Do something specific for the PAGE2 } else { //Do something for all other pages } } } } I know it's not the perfect solution, but I hope it will help you somehow.

zokibtmkd
  • 2,173
  • 1
  • 22
  • 24
  • That won't work, because "another output" will still be put out by the second page... – Peter Oct 04 '14 at 20:13
  • That's why I mentioned that he needs to write the output in the method itself and call the method in the template without doing if/else logic: $z->XYZ(); – zokibtmkd Oct 04 '14 at 20:48
  • But the if/else logic will not disappear... That is his problem! – Peter Oct 04 '14 at 20:52
  • Then there is no other way to fix this, not without changing the template via the Block itself or the XML layout. – zokibtmkd Oct 04 '14 at 20:55
  • You are right. But he told that he cannot alter the template. So I see no solution... – Peter Oct 04 '14 at 20:59
-2

The page number has to be specified somewhere in the $_GET or the $_POST array (or in another query). Check that variable and implement the alternative logic.

Shawn Patrick Rice
  • 800
  • 10
  • 17