Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Definiowanie składowej poprzez objekt klasy
Raven1122
post
Post #1





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

Ostrzeżenie: (0%)
-----


Witam,

Mam pytanie, jak przekazać do klasy View składową, np.:

$this->view->title = 'Strona główna';

, a potem

wykorzystać tą składową w klasie View, np.:

echo $this->title;


Kawałki mojego kodu:

Klasa View

  1. <?php
  2.  
  3. class View {
  4.  
  5. function __construct() {
  6.  
  7. }
  8.  
  9. public function render($name){
  10. require BASE_URL.'views\\'.$name.'.php';
  11. }
  12.  
  13. }


Klasa nadrzędna kontrolera(każdy kontroler ją przedłuża):
  1. <?php
  2.  
  3.  
  4. class Controller {
  5.  
  6. function __construct() {
  7. $this->view = new View();
  8. }
  9.  
  10. }


i kontroller np. strony głównej

  1. <?php
  2.  
  3. class Index extends Controller{
  4.  
  5. function __construct() {
  6. parent::__construct();
  7. }
  8.  
  9. function index(){
  10. $this->view->render('index\index');
  11. $this->view->title = 'Strona główna';
  12. }
  13.  
  14. }
  15.  
  16. ?>


No i View strony głównej:
  1. <?php
  2.  
  3. require 'public/templates/header.php';
  4. ?>
  5. <hr />
  6. <?php
  7. echo $this->title;
  8. ?>
  9. <hr />
  10. <?php
  11. require 'public/templates/footer.php';
  12. ?>
  13.  


Po uruchomieniu strony głównej uzyskuje:

Notice: Undefined property: View::$title in D:\wamp\www\MVC\views\index\index.php on line 7

Proszę o pomoc (IMG:style_emoticons/default/smile.gif)
Z góry dziękuje.

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 12)
prz3kus
post
Post #2





Grupa: Zarejestrowani
Postów: 260
Pomógł: 30
Dołączył: 22.01.2007

Ostrzeżenie: (0%)
-----


Wywołujesz gdzieś tą funkcje index? Nie wiem co ten kod robi ale do MVC mu daleka droga ;P Komunikat to informacja ze używasz zmiennej której nie zadeklarowałeś -> http://php.net/manual/en/function.isset.php
Go to the top of the page
+Quote Post
memory
post
Post #3





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


Musisz użyć magicznych funkcji __set(), __get()


Ten post edytował memory 20.08.2013, 10:55:54
Go to the top of the page
+Quote Post
Raven1122
post
Post #4





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

Ostrzeżenie: (0%)
-----


prz3kus, nie powiedzialem ze to jest caly kod -.-

Oczywiscie ze mam jeszcze klase Bootstrap w ktorej mam wszystko wywolane,

@UP, Jak użyć takich _set i _get?
Go to the top of the page
+Quote Post
Wazniak96
post
Post #5





Grupa: Zarejestrowani
Postów: 550
Pomógł: 75
Dołączył: 5.06.2012
Skąd: Lębork

Ostrzeżenie: (0%)
-----


Pierwszy link z googla Klik (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
sajegib
post
Post #6





Grupa: Zarejestrowani
Postów: 352
Pomógł: 59
Dołączył: 16.01.2013

Ostrzeżenie: (0%)
-----


wykorzystanie w klasie:

<?php

  1. class View {
  2.  
  3. public $view;
  4.  
  5. function __construct() {
  6.  
  7. }
  8.  
  9. function echo(){
  10. echo $this->view;
  11. }
  12.  
  13.  
  14. public function render($name){
  15. require BASE_URL.'views\\'.$name.'.php';
  16. }
  17.  
  18. }



przekazanie do klasy:

  1. $view = new view($zmienna_do_przekazania);



Cytat
Musisz użyć magicznych funkcji __set(), __get()



Musi to na rusi (IMG:style_emoticons/default/smile.gif)

Ten post edytował sajegib 20.08.2013, 11:12:04
Go to the top of the page
+Quote Post
prz3kus
post
Post #7





Grupa: Zarejestrowani
Postów: 260
Pomógł: 30
Dołączył: 22.01.2007

Ostrzeżenie: (0%)
-----


to juz lepiej co nie znaczy ze to nadal bez sensu (IMG:style_emoticons/default/tongue.gif)
  1. <?php
  2.  
  3.  
  4. class Controller {
  5. private view;
  6. function __construct() {
  7. $this->view = new View();
  8. }
  9.  
  10. }
  11.  
  12. public function Link($name)
  13. {
  14. $this->view.render($name);
  15. }

Go to the top of the page
+Quote Post
Raven1122
post
Post #8





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

Ostrzeżenie: (0%)
-----


Prz3kus wez stad wyjdz, prosze

Uzylem __set i __get i nadal nic nie dostaje, oprócz tego, że błąd zniknął.
Go to the top of the page
+Quote Post
styryl
post
Post #9





Grupa: Zarejestrowani
Postów: 223
Pomógł: 27
Dołączył: 16.04.2008
Skąd: Bakutilu

Ostrzeżenie: (0%)
-----


Bardzo prosty przykład:

  1. class View
  2. {
  3. public function __construct( $filename )
  4. {
  5. $this->filename = $filename;
  6. }
  7.  
  8. public function set( $key, $value )
  9. {
  10. $this->_data[ $key ] = $value;
  11. }
  12.  
  13. public function render()
  14. {
  15. extract( $this->_data, EXTR_SKIP );
  16.  
  17.  
  18.  
  19. try
  20. {
  21. // Load the view within the current scope
  22. include $this->filename;
  23. }
  24. catch (Exception $e)
  25. {
  26. // Delete the output buffer
  27.  
  28. // Re-throw the exception
  29. throw $e;
  30. }
  31.  
  32. // Get the captured output and close the buffer
  33. return ob_get_clean();
  34. }
  35.  
  36. }
  37.  
  38. $view = new View('test.php');
  39. $view->set('test_var', 'działa');
  40. echo $view->render();
  41.  
  42. $view2 = new View('test2.php');
  43. $view2->set('test_var2', 'działa2');
  44. echo $view2->render();
Go to the top of the page
+Quote Post
Wazniak96
post
Post #10





Grupa: Zarejestrowani
Postów: 550
Pomógł: 75
Dołączył: 5.06.2012
Skąd: Lębork

Ostrzeżenie: (0%)
-----


Pokaż kod...
Go to the top of the page
+Quote Post
Raven1122
post
Post #11





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

Ostrzeżenie: (0%)
-----


Klasa View:

  1. <?php
  2.  
  3. class View {
  4.  
  5. private $data = array();
  6. function __construct() {
  7.  
  8. }
  9.  
  10.  
  11. public function __set($nazwa, $wartosc) {
  12. $this->data[$nazwa] = $wartosc;
  13. }
  14. public function __get($nazwa) {
  15. if (array_key_exists($nazwa, $this->data)) {
  16. return $this->data[$nazwa];
  17. } else {
  18. return false;
  19. }
  20. }
  21.  
  22. public function render($name){
  23. require BASE_URL.'views\\'.$name.'.php';
  24. }
  25.  
  26. }


View dla index
  1. <?php
  2.  
  3. require 'public/templates/header.php';
  4. ?>
  5. <hr />
  6. <?php
  7. echo $this->msg;
  8. ?>
  9. <hr />
  10. <?php
  11. require 'public/templates/footer.php';
  12. ?>
  13.  
  14.  
  15.  
  16.  


Controller dla index

  1. <?php
  2.  
  3. class Index extends Controller{
  4.  
  5. function __construct() {
  6. parent::__construct();
  7. }
  8.  
  9. function index(){
  10. $this->view->render('index\index');
  11. $this->view->msg = 'Strona główna';
  12. }
  13.  
  14. }
  15.  
  16. ?>

Go to the top of the page
+Quote Post
Wazniak96
post
Post #12





Grupa: Zarejestrowani
Postów: 550
Pomógł: 75
Dołączył: 5.06.2012
Skąd: Lębork

Ostrzeżenie: (0%)
-----


Najpierw przypisz dane do $this-> viev-> msg, dopiero później wykonaj metodę render. Kiedy próbujesz wyświetlić treść, ona jeszcze nie istnieje (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
Raven1122
post
Post #13





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

Ostrzeżenie: (0%)
-----


Dzięki, działa (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 4.10.2025 - 11:48