Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Namespace i autoloading
elbroth
post 20.04.2016, 14:16:23
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 20.04.2016

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


Witam, mam problem z autoloadingiem i namespace'ami.

Poniżej kod:

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


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
Go to the top of the page
+Quote Post
Lion
post 22.06.2016, 11:34:01
Post #2





Grupa: Zarejestrowani
Postów: 148
Pomógł: 14
Dołączył: 23.02.2013

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


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


--------------------
Go to the top of the page
+Quote Post
com
post 22.06.2016, 13:23:24
Post #3





Grupa: Zarejestrowani
Postów: 3 033
Pomógł: 366
Dołączył: 24.05.2012

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


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?

Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 28.04.2024 - 12:42