Zend_Layout + helperClass - php

I am try to use helperClass in Zend_Layout options.
I create a class:
class Helper_Testi extends Zend_Controller_Action_Helper_Abstract{
public function init(){
echo "111111111";
$this->fff = 'hello from Helper';
}
public function getMessage(){
echo "==============";
return "message";
}
}
and in Bootstrap.php try add it to Zend_Layout:
$options = array('layout' => 'layout','helperClass'=>'../application/controllers/helper/Testi');
$layout = new Zend_Layout();
$layout->startMvc($options);
But when I reload browser I see Exception:
Fatal error: Uncaught exception 'Zend_Exception' with message 'File "../application/controllers/helper/Testi.php" does not exist or class "../application/controllers/helper/Testi" was not found in the file'
What I do wrong? Help me please.

Try to use APPLICATION_PATH constant in your path

You need place your helper class in correct place.
E.g. in my bootsrap there are such lines:
protected function _initViewHelpers() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->addHelperPath('Icc/View/Helper' , 'Icc_View_Helper');
}
And helper files will be located in this folder: Icc\View\Helper\
Edit:
E.g. I have file FormDropdown.php
with such content:
class Icc_View_Helper_FormDropdown extends Zend_View_Helper_Abstract {
function formDropdown($name = '')
{
return "<select name='$name' id='$name'></select>";
}
}
In view I can use this helper in this way:
<?=$this->formDropdown('icc_info_salutation')?>

Related

How do I execute a function on a string PHP

While i was working on a school project I ran into a problem.
I have made an Router class where it get the right files, but I am getting the following error:
Fatal error: Method mvc\App::__toString() must not throw an exception, caught Error: Call to a member function getHTML() on string in /opt/lampp/htdocs/School/testPHP/mvc/public/index.php on line 0
The echo returns the correct file path so that is not the issue I'm running into.
I think the issue is because I try to execute an function into a string.
Does anyone know how I can fix my issue?
App Class:
<?php
namespace mvc;
class App{
private $router;
public function __construct(){
$this->router = new \mvc\Router();
}
public function __toString(){
try {
echo $this->router->getView(); //this returns the correct file path
return $this->router->getView()->getHTML();
} catch (Exception $e) {
return $e.getMessage;
}
}
}
?>
Router.php:
<?php
namespace mvc;
class Router{
private $route;
private $view;
private $controller;
private $model;
public function __construct(){
require_once(LOCAL_ROOT. "php/Routes.php");
if (isset($_GET['route'])){
$this->route = explode("/" , $_GET['route']);
}
$route = isset($routes[$this->getRoute()])? $this->getRoute() : DEFAULT_ROUTE;
$this->controller = "\\controllers\\". $routes[$route]['controller'];
$this->view = "\\views\\". $routes[$route]['view'];
// $model = "\\models\\". $routes[$route]['model'];
}
private function getRoute(){
return count($this->route) > 0 ? $this->route[0] : DEFAULT_ROUTE;
}
public function getView(){
return $this->view;
}
}
?>
Routes.php
<?php
define("DEFAULT_ROUTE", "home");
$routes = array(
"home" => array(
"view" => "HomeView",
"controller" => "HomeController",
),
"form" => array(
"view" => "FormView",
"controller" => "FormController",
),
"test" => array(
"view" => "TestView",
"controller" => "TestController",
),
)
?>
TestView.php
<?php
namespace views;
class TestView extends \mvc\View{
public function getHTML(){
// return 'dit is testView';
$klik = $this->controller->getGetData("klik");
$output = "";
$output .= "<h1>".$klik++ ."</h1>";
$output .= "klik";
$output .= "<br>";
return $output;
}
}
?>
The Problem:
Okay so the problem is that you're making a call to a function from a string.
This is the getView method in the Router class:
public function getView(){
return $this->view;
}
This method returns a string:
$this->view = "\\views\\". $routes[$route]['view'];
You are then trying to call a method from this string:
return $this->router->getView()->getHTML();
The Potential Solution:
Obviously you're trying to access the getHTML method in the TestView class, so without knowing more about your setup, it's hard to get exact, but I'll take a guess at the following and you can guide me in the right direction in the comments.
If you change the constructor in your App class to this:
public function __construct(){
$this->router = new \mvc\Router();
$this->testView = new \views\TestView();
}
Then you can change your __toString method to the following:
echo $this->router->getView(); //this returns the correct file path
return $this->testView->getHTML();
I'm not sure why you'd echo and then return, usually it's one or the other, but if you elaborate more on your expected output I can help you some more, or hopefully my explanation has given you enough to work from.
Solution:
After reading your comments, it looks like you want to instantiate the class from the result of the getView method, you can do this by setting the result into a variable and calling a new class from that string and THEN calling the method from that class.
$class = $this->router->getView();
$class = new $class;
return $class->getHTML();

How to initialize instance properties?

I have sample code in my Class:
public $themePath = "layouts/inbox/";
public $theme = $this->themePath."theme-limitless.";
public $inboxView = $this->theme."inbox";
And here my code not work. I must save my theme name in one var and path to theme in another var and use view using theme name. Now I have error:
Constant expression contains invalid operations:
public $theme = $this->themePath."theme-limitless."; // Error line
Generaly I must get path in var $inboxView:
$inboxView = "layouts/inbox/theme-limitless.inbox";
You can initializate class properties in __construct method:
class MyClass
{
public $themePath = "layouts/inbox/";
public $theme;
public $inboxView;
function __construct() {
$this->theme = $this->themePath . "theme-limitless.";
$this->inboxView = $this->theme . "inbox";
}
}

using same namespace php, Call to undefined function

using same namespace php
I have this files in the same folder :
OtherFunctions.php
<?php
namespace Pack\sp;
$Tble = NULL;
function SetTble($tble) {
global $Tble;
$Tble = $tble;
}
function GetTble() {
global $Tble;
return $Tble;
}
function Funct0($Str0, $Str1) {
return $Str0 == $Str1;
}
function Funct1($Arg) {
return "The Value is ".$Arg;
}
//... from 0 to 16
function Funct16($Arg) {
return "The Value is ".$Arg;
}
?>
How to call all functions contained in this file?
In one class File SubClass.php I have this:
<?php
namespace Pack\sp;
class SubClass {
public $CArg = "";
}
?>
In other class File LeadClass.php
I have this:
<?php
namespace Pack\sp;
use \Pack\sp\SubClass;
require_once("OtherFunctions.php");
class LeadClass {
public function __construct($Name) {
echo("_._");
$NewSC = new SubClass();
$NewSC->CArg = $Name;
SetTble($Name);
echo("ini:".GetTble().":end");
}
}
?>
I want call all function in one instruction of OtherFunctions.php File, but I don't kno how to do it....
I trying to replicate this message in other code
Fatal error: Call to undefined function GetTble() in C:...\LeadClass.php on line 10
But, I'm obtaining blank page
EDIT
Was added the line:
require_once("OtherFunctions.php");
And was replaced the line:
require_once("SubClass.php");
by the line:
use \Pack\sp\SubClass;
in LeadClass.php File.
But, I'm obtaining blank page
You need to add the next line
namespace Pack\sp;
use \Pack\sp\SubClass; // <--- add this
Also I think you should put the functios of the OtherFunctions file into a new class link
namespace Pack\sp;
class OtherFunctions{
// your current code goes here
}
After that you need to extend the SubClass whit the OtherFunctios class
namespace Pack\sp;
use Pack\sp\OtherFunctions;
class SubClass extends OtherFunctions {
public $CArg = "";
}
EDIT
I just tried your code and I can make the LeasClass to work as follow
<?php
namespace Pack\sp;
require_once("OtherFunctions.php");
require_once("SubClass.php");
class LeadClass {
public function __construct($Name) {
echo("_._");
$NewSC = new SubClass();
$NewSC->CArg = $Name;
SetTble($Name);
echo("ini:".GetTble().":end");
}
}
$LeadClass = new LeadClass('table');
?>
Have you already initialize the class?

PHP OOP error that I cannot understand

I try to extend the CheckfrontAPI class with my new class.
In my case I use the Singleton pattern in order to load only one instance at a time of my class and I get that error
Fatal error: Declaration of CheckFrontIntegrator::store() must be compatible with that of CheckfrontAPI::store() in /home/my_web_site/public_html/wp-content/plugins/checkfront/class/Checkfront_Integration.php on line 83
Any idea on how to solve that issue ?
Here is the CheckfrontAPI source code : https://github.com/Checkfront/PHP-SDK/blob/master/lib/CheckfrontAPI.php
And here is my class that extends that class:
<?php
class CheckFrontIntegrator extends CheckfrontAPI
{
private static $instance = null;
public $tmp_file = '.checkfront_oauth';
final protected function store($data = array())
{
$tmp_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR. $this->tmp_file;
if(count($data))
{
file_put_contents(
$tmp_file,
json_encode(
$data,
true
)
);
}
elseif(is_file($tmp_file))
{
$data = json_decode(
trim(
file_get_contents(
$tmp_file
)
),
true
);
}
return $data;
}
public function session($session_id, $data = array())
{
$_SESSION['checkfront']['session_id'] = $session_id;
}
public static function instance($data)
{
if(!isset(self::$instance))
{
self::$instance = new CheckFrontIntegrator($data);
}
return self::$instance;
}
public function __construct($data)
{
if(session_id() == '')
{
session_start();
}
parent::__construct($data, session_id());
}
}
?>
And I initiate the new instance of that class like that:
$this->checkfront_integrator = CheckFrontIntegrator::instance($args);
where args are all the important information needit by the class to initiate a new object
AFTER EDIT
I have change my method store from:
final protected function store($data = array())
....
to
protected function store($data)
....
and the problem still occure :(
CheckfrontAPI is an abstract class? in this case your CheckFrontIntegrator::store() arguments count must be identical to original declaration
EDIT
I see on github
abstract protected function store($data);
your override must be:
protected function store($data) {
}
You are extending CheckfrontAPI. CheckfrontAPI has a method store(). If you override that method you must do it properly.
Post the code of CheckfrontAPI and your class Checkfront_Integration: when can understand what's the problem.
When you want to extent the functionality of an existing class by writing your own class and the class you are extending is is an abstract one, you'll need to make sure that the function calls are compatible.
What does this mean?
If the class you are extending has this function call for example :
function walk($direction, $speed = null);
Then you will have to honor the function signature in your implementation - that means you'll still have to have to pass two function arguments in your version.
You will not be able to alter is to be like this :
function walk($direction, $speed, $clothing);

Call to undefined method CI_Controller::Controller()

I have got this controller:
class Start extends CI_Controller{
var $base;
var $css;
function Start()
{
parent::Controller(); //error here.
$this->base = $this->config->item('base_url'); //error here
$this->css = $this->config->item('css');
}
function hello($name)
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['mytitle'] = 'Welcome to this site';
$data['mytext'] = "Hello, $name, now we're getting dynamic!";
$this->load->view('testView', $data);
}
}
it tells me in this line:
parent::Controller(); //error here.
Call to undefined method CI_Controller::Controller()
If I remove that line..I get an error for the next line that says..
Call to a member function item() on a non-object
How do I prevent such errors form happening?
If you're using CI 2.x then your class constructor should look like this:
public function __construct()
{
parent::__construct();
// Your own constructor code
}
read more in user guide
In CodeIgniter 2, the constructor is named __constructor and not the class name. So you need to call parent::__construct() instead of parent::Controller()
Here's an article that you can read that shows one major difference between CodeIgniter 1.x and CodeIgniter 2.x
http://ulyssesonline.com/2011/03/01/differences-between-codeigniter-1-7-2-and-2-0-0/
If you're running your Codeigniter project via Xampp or a similar server add the following code to the bottom of your config.php file in the following directory; ci_project/application/config/config.php
function my_load($class) {
if (strpos($class, 'CI_') !== 0) {
if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {
require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');
}
}
}
spl_autoload_register('my_load');
The above code would help to; load class in core folder.
I'm certain this works in the following setup; CI-3+, Xampp, Php5.6, and or 5.6+
Also, you can then decide to create and allow other classes to reference your own Controller (which extends the original CI_Controller) by creating a file named MY_Controller.php in the following directory: ci_project/application/core/ and adding the following code in it;
<?php
class MY_Controller extends CI_Controller {
}
?>
That way you can always just reference or extend the other Classes to your own Controller (MY_Controller) throughout the rest of the project e.g.
class Admin extends MY_Controller {
//your function here
}
I hope this helps.

Categories