Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ Object-oriented programming _ Namespace i autoloading

Napisany przez: elbroth 20.04.2016, 14:16:23

Witam, mam problem z autoloadingiem i namespace'ami.

Poniżej kod:

* index.php

  1. <?php
  2. http://www.php.net/session_start();
  3.  
  4. require 'vendor/autoload.php';
  5. use View\Template\First\Template;
  6. use Shop\User;
  7. use Shop\SessionsAndCookies;
  8.  
  9.  
  10.  
  11. $template = new Template;
  12. $template -> siteStart();
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. $user = new User;
  20. $sac = new SessionsAndCookies;
  21.  
  22. http://www.php.net/echo' Zalogowany? '. $user -> isLogged();
  23. http://www.php.net/echo'<br><br>';
  24. http://www.php.net/print $_SESSION['logged'];
  25. http://www.php.net/print $_SESSION['email'];
  26.  
  27. http://www.php.net/print'<br>';
  28. http://www.php.net/print $sac -> getSession('logged');
  29. http://www.php.net/print $sac -> getSession('email');
  30.  
  31.  
  32. $product = new Product;
  33.  
  34.  
  35.  
  36.  
  37. $category = new Categories;
  38. $category -> set('name', 'Main', 1);
  39.  
  40.  
  41.  
  42. $template -> siteEnd();



* composer.json
  1. {
  2. "autoload": {
  3. "psr-4": {
  4. "Shop\\": "class",
  5. "View\\": "view"
  6. }
  7. }
  8. }


* View/Template/First/template.php
  1. <?php
  2.  
  3. namespace View\Template\First;
  4.  
  5. class Template {
  6. private $title;
  7. private $footer;
  8.  
  9. public function __construct($TypeOfView = null)
  10. {
  11. $this -> title = 'Shop';
  12. $this -> footer = '';
  13. $this -> logotype = '';
  14. $this -> view = $TypeOfView;
  15. }
  16.  
  17. public function menu()
  18. {
  19. $db = new Database;
  20. $db -> query('SELECT * FROM categories WHERE visibility = 1 ORDER BY name ASC');
  21. $result = $db -> fetchAll();
  22.  
  23. return $result;
  24. }
  25.  
  26.  
  27. public function siteStart()
  28. {
  29. http://www.php.net/echo'
  30. <!DOCTYPE html>
  31. <html>
  32. <html lang="pl-PL">
  33. <head>
  34. <meta charset="utf-8">
  35. <meta name="description" content="...">
  36. <meta name="keywords" content="...">
  37. <link rel="stylesheet" href="View/Template/First/css/style.css">
  38. <title>'.$this -> title.'</title>
  39.  
  40. <!-- [if lt IE 9]>
  41. <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
  42. <![endif-->
  43. </head>
  44. <body>
  45. <header>
  46. <section class="logo">
  47. Nazwa strony
  48. </section>
  49. <section class="basket">
  50. Koszyk: ( x )
  51. </section>
  52. <nav>
  53. <ul>
  54. <li><a href="Index/">Strona główna</a></li>
  55. <li><a href="">O nas</a></li>
  56. <li><a href="">Pomoc</a></li>
  57. <li><a href="">Kontakt</a></li>
  58. </nav>
  59. </header>
  60. <aside>
  61. <ul class="menu">';
  62.  
  63. if($this -> view == 'Admin')
  64. {
  65. $directory = 'Class/Admin/';
  66. $files = http://www.php.net/array_diff(scandir($directory), http://www.php.net/array('..', '.'));
  67. foreach($files as $file)
  68. {
  69. http://www.php.net/echo '<a href="admin?action='.http://www.php.net/trim($file,'.php').'"">'.http://www.php.net/trim($file, '.php').'</a></br>';
  70. }
  71. }
  72. else
  73. {
  74. $menu = $this->menu();
  75. foreach ($menu as $r)
  76. {
  77. http://www.php.net/echo'<li><a href="Products?category='.$r['name'].'">'.$r['name'].'</a></li>';
  78. }
  79. }
  80.  
  81. http://www.php.net/echo' </ul>
  82. ';
  83.  
  84.  
  85.  
  86. if(http://www.php.net/isset($_SESSION['logged']) && $_SESSION['logged'] == true && !http://www.php.net/empty($_COOKIE['logged']))
  87. {
  88. http://www.php.net/echo'<br>panel<br><br><a href="Admin/">Panel admina</a><Br> <a href="User?action=logout">Wyloguj</a><br>';
  89. }
  90. else
  91. {
  92. http://www.php.net/echo'<br>
  93. <a href="User/">Zaloguj się</a><br>
  94. <a href="User?action=register">Zarejestruj się</a><br>
  95. ';
  96. }
  97.  
  98. http://www.php.net/echo'
  99. </aside>
  100. <main>
  101. ';
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. public function siteEnd()
  109. {
  110. http://www.php.net/echo'
  111. </main>
  112.  
  113. <footer>
  114. '.$this -> footer.'
  115. </footer>
  116. </body>
  117. </html>
  118. ';
  119. }
  120.  
  121. }
  122. ?>


Pytanie nr 1 - jak uzyskać dostęp do klasy Database, tak abym mógł jej użyć w klasie Template?
Pytanie nr 2 - jeśli pozbędę się kody z template.php, dostaję błąd, że nie znaleziono klasy User w pliku index.php, czyli coś ewidentlnie nie działa, a ja nie mam pojęcia, co jest zle z moim kodem.

Z góry dzięki za pomoc wink.gif

Napisany przez: Lion 22.06.2016, 11:34:01

Mało napisałeś o tej klasie Database ale być może wystarczy \Database.

Napisany przez: com 22.06.2016, 13:23:24

Cytat
Pytanie nr 1 - jak uzyskać dostęp do klasy Database, tak abym mógł jej użyć w klasie Template?

Poprzez use tak jak w index dla innych. Chyba że znajduje się w tej samej przestrzeni, to nie musisz dodawać nic.

Odp na pytanie drugie a gdzie tak klasa się znajduje?


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)