PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи

Страниц (1): [1]

> Найдено сообщений: 6
dot Отправлено: 04 Января, 2017 - 13:29:07 • Тема: Выбрасывается ошибка Argument 1 passed to RequestRegistry::setRequest() must be an instance of mycontrollers\Request • Форум: Объектно-ориентированное программирование

Ответов: 2
Просмотров: 2110
хмм да похоже так и есть спасибо большое !!! Улыбка Тут поправил Улыбка
dot Отправлено: 04 Января, 2017 - 12:44:48 • Тема: Выбрасывается ошибка Argument 1 passed to RequestRegistry::setRequest() must be an instance of mycontrollers\Request • Форум: Объектно-ориентированное программирование

Ответов: 2
Просмотров: 2110
Здравствуйте скажите как поправить вот такую вот беду , вот полный текст ошибки Catchable fatal error: Argument 1 passed to RequestRegistry::setRequest() must be an instance of mycontrollers\Request, instance of Request given, called in C:\xampp\htdocs\PhpProject2\mycontrollers\Request.php on line 21 and defined in C:\xampp\htdocs\PhpProject2\RequestRegistry.php on line 46
и дальше сами классы
CODE (htmlphp):
скопировать код в буфер обмена
  1. include_once 'RequestRegistry.php';
  2. class Request {
  3.     private $properties;
  4.     private $feedback = array();
  5.    
  6.     function __construct() {
  7.         $this->init();
  8.         RequestRegistry::setRequest($this);
  9.     }
  10.    
  11.     function init() {
  12.         if(isset($_SERVER['REQUEST_METHOD']))
  13.         {
  14.             $this->properties = $_REQUEST;
  15.             return;
  16.         }
  17.         foreach ($_SERVER['argv'] as $arg) {
  18.             if(strpos($arg, '='))
  19.             {
  20.                 list($key,$val)= explode("=", $arg);
  21.                 $this->setProperty($key,$val);
  22.             }
  23.         }
  24.     }
  25.    
  26.     function getProperty($key){
  27.         if(isset($this->properties[$key]))
  28.         {
  29.             return $this->properties[$key];
  30.         }
  31.     }
  32.    
  33.     function setProperty($key,$val) {
  34.         $this->properties[$key] = $val;
  35.     }
  36.    
  37.     function addFeedback($msg) {
  38.         array_push($this->feedback, $msg);
  39.     }
  40.    
  41.     function getFeedback() {
  42.         return $this->feedback;
  43.     }
  44.    
  45.     function getFeedBackString($separator="\n") {
  46.         return implode($separator, $this->feedback);
  47.     }
  48. }
  49.  
  50. require_once 'Registry.php';
  51.  class RequestRegistry extends Registry{
  52.      private $values =array();
  53.      private static $instance;
  54.      private function __construct() {
  55.     }
  56.    
  57.     static function instance(){
  58.         if(!isset(self::$instance))
  59.         {
  60.             self::$instance = new self();
  61.         }
  62.         return self::$instance;
  63.     }
  64.    
  65.     protected function get($key) {
  66.         if(isset($this->values[$key]))
  67.         {
  68.             return $this->values[$key];
  69.         }
  70.         return null;
  71.     }
  72.    
  73.     protected function set($key, $value) {
  74.         $this->values[$key] = $value;
  75.     }
  76.    
  77.     static function getRequest()
  78.     {
  79.         return self::instance()->get('request');
  80.     }
  81.    
  82.     static function setRequest(\mycontrollers\Request $request) {
  83.         return self::instance()->set('request', $request);
  84.     }
  85. }
  86.  
  87.  

Я думаю что ошибка возникает изза того что в конструкторе класса Request когда передается этот же объект
(function __construct() {
$this->init();
RequestRegistry::setRequest($this);
}) Request он не создается до конца и соответственно выскакивает ошибка написанная выше. Так ли это или нет и как ее исправить ?
dot Отправлено: 04 Января, 2017 - 11:17:53 • Тема: Ошибка Using $this when not in object context • Форум: Объектно-ориентированное программирование

Ответов: 6
Просмотров: 2951
Мелкий пишет:
Объект вам нужен, объект.
PHP:
скопировать код в буфер обмена
  1. $Manager = new PageControllerManager;
  2. $Manager->process()


Спасибо это помогло , не понятно правда почему, но там тут хоть заработало. Щас буду с другими ошибками разбираться. Спасибо огромное за помощь Улыбка
dot Отправлено: 04 Января, 2017 - 11:05:38 • Тема: Ошибка Using $this when not in object context • Форум: Объектно-ориентированное программирование

Ответов: 6
Просмотров: 2951
хмм вот так $request = $this::getRequest(); ?? тогда он выдает Notice: Undefined variable:

Fatal error: Class name must be a valid object or a string и ругается на эту строку , скажите это означает что $request пустая переменная ?
dot Отправлено: 04 Января, 2017 - 10:15:15 • Тема: Ошибка Using $this when not in object context • Форум: Объектно-ориентированное программирование

Ответов: 6
Просмотров: 2951
Вызывается так PageControllerManager::process();
находится в index.php
dot Отправлено: 04 Января, 2017 - 08:43:01 • Тема: Ошибка Using $this when not in object context • Форум: Объектно-ориентированное программирование

Ответов: 6
Просмотров: 2951
Добрый день только осваиваю программирование на php и столкнулся вот с такой вот проблемой в коде на $request = $this->getRequest(); выдает ошибку Ошибка Using $this when not in object context
Подскажите плиз что я не так делаю ?
Ниже полный код

CODE (htmlphp):
скопировать код в буфер обмена
  1. class PageControllerManager extends PageController {
  2.    
  3.     public function process() {
  4.         try
  5.         {
  6.                 $request = $this->getRequest();
  7.                 $name = $request->getProperty('userName');
  8.                 if(is_null($request->getProperty('submitted')))
  9.                 {
  10.                     $request->addFeedback('Вы не авторизовались в системе');
  11.                     $this->forward('Registration.php');
  12.                 }
  13.                 $this->forward('UserCaller.php');
  14.         }
  15.         catch (Exception $exc)
  16.         {
  17.             echo $exc->getTraceAsString();
  18.         }
  19.  
  20.        
  21.     }
  22.  
  23. }
  24.  
  25.  
  26.  
  27. abstract class PageController {
  28.     private $request;
  29.     function __construct() {
  30.         $request = RequestRegistry::getRequest();
  31.         if(is_null($request))
  32.         {
  33.             $request = new Request();
  34.         }
  35.         $this->request = $request;
  36.     }
  37.    
  38.     abstract function process();
  39.     function forward($resource) {
  40.         include($resource);
  41.         exit(0);
  42.     }
  43.    
  44.     function getRequest()
  45.     {
  46.         return $this->request;
  47.     }
  48. }
  49.  
  50. class RequestRegistry extends Registry{
  51.     private function __construct() {
  52.     }
  53.    
  54.     static function instance(){
  55.         if(!isset(self::instance()))
  56.         {
  57.             self::instance() = new self();
  58.         }
  59.         return self::instance();
  60.     }
  61.    
  62.     protected function get($key) {
  63.         if(isset($this->values[$key]))
  64.         {
  65.             return $this->values[$key];
  66.         }
  67.         return null;
  68.     }
  69.    
  70.     protected function set($key, $value) {
  71.         $this->values[$key] = $value;
  72.     }
  73.    
  74.     static function getRequest()
  75.     {
  76.         return self::instance()->get('request');
  77.     }
  78.    
  79.     static function setRequest(\mycontrollers\Request $request) {
  80.         return self::instance()->set('request', $request);
  81.     }
  82. }
  83.  
  84. abstract class Registry {
  85.     abstract protected function get($key);
  86.     abstract protected function set($key,$value);
  87. }
  88.  

Страниц (1): [1]
Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB