Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Widocznosc/Dostepnosc wlasciwosci - wybiorczo, Wlasciwosc w klasie /
JakubBab
post 9.03.2016, 22:23:05
Post #1





Grupa: Zarejestrowani
Postów: 96
Pomógł: 2
Dołączył: 13.07.2015

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


Czesc,

Taka oto zagwozdka:

- przypisuje wlasciwosci wartosc w funkcji. Jest ona widoczna w innej klasie nastomiast w drugiej ma on wartosc null. Why?! Chodzi o -> protected $temp;

  1.  
  2.  
  3. .....
  4.  
  5. protected $temp;
  6. public function tableTh() {
  7.  
  8. for ($i = 0; $i <= 7; $i++) {
  9.  
  10. if ($i == 0) {
  11. $this->tableTh.='<td width="25px" style="background: #c0c0c0;">X</td>';
  12. } else if ($i == 1) {
  13. // -7 because it has to get the day commencing the week - monday
  14.  
  15. $this->tableTh .= '<td style="background: #c0c0c0;">' . $this->giveCalendarName() . "</br>" . $this->giveCalendarNumber('null',7) . '</td>';
  16. $this->temp= $this->giveCalendarNumber(7,$i-7);
  17.  
  18. } else if ($i > 1) {
  19. // -7 because it has to get the day commencing the week - monday
  20. $this->tableTh .= '<td style="background: #c0c0c0;">' . $this->giveCalendarName($i - 1) . "</br>" . $this->giveCalendarNumber(7,$i-1) . '</td>';
  21. $this->temp= $this->giveCalendarNumber(7,$i-7);
  22.  
  23. }
  24. }
  25.  
  26. return $this->tableTh;
  27. }


Tutaj (ponizej) ma rece i nogi i po wywolaniu dostaje wartosc taka jaka chce czyli - (w tym konkretnym przypadku) string 07.03.2016.

  1. public function tableRow() {
  2.  
  3.  
  4. for ($i = 1; $i <= 8; $i++) {
  5. echo "<tr></tr>";
  6.  
  7. for ($j = 0; $j <= 7; $j++) {
  8.  
  9. if ($j == 0) {
  10. echo '<td width="30px" style="background: #c0c0c0;">' . $this->giveTime(9 + $i - 1) . '</br></td>';
  11. } else {
  12.  
  13. echo '<td><input type="checkbox" name="Reservation[' . $j . ']['.$this->temp.'][' . $i . ']" value="1"></br></td>';
  14. }
  15. }
  16. }
  17.  
  18. }


Natomiast gdy chce uzyc tej samej wlasciwosci ($this->temp) w innej funkcji ma ona wartosc NULL. Jakies pomysly?

Ten post edytował JakubBab 9.03.2016, 22:24:09
Go to the top of the page
+Quote Post
Rysh
post 9.03.2016, 22:40:02
Post #2





Grupa: Zarejestrowani
Postów: 821
Pomógł: 111
Dołączył: 11.09.2006
Skąd: Biała Podlaska

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


Najpierw musisz wywołać pierwszą funkcję aby coś dla $this->temp przypisało, dopiero później możesz używać jej w kolejnych funkcjach.


--------------------
Go to the top of the page
+Quote Post
JakubBab
post 9.03.2016, 23:07:07
Post #3





Grupa: Zarejestrowani
Postów: 96
Pomógł: 2
Dołączył: 13.07.2015

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


Nie wiem czy dobrze rozumiem...

Wywolana w sensie return w klasie czy wywolana juz w widoku?

Jesli w widoku to juz smiga( tak jak napisalem).

Przypisalem do innej wlasciwosci i wywolalem w klasie i smiga. Pozniej uzywam jako funckji a nie jako wlasciwosci.


  1.  
  2. public function tester(){
  3.  
  4.  
  5. for($i = 0; $i <= 7; $i++){
  6.  
  7. if ($i >= 0){
  8. $this->startTime = $this->giveCalendarNumber(7,$i-7);
  9. }
  10. }
  11. return $this->startTime;
  12. }


Ten post edytował JakubBab 10.03.2016, 10:01:14
Go to the top of the page
+Quote Post
Pyton_000
post 10.03.2016, 08:07:22
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


To może pokaż kod który nie działa? Bo strasznie zamieszałeś.
Go to the top of the page
+Quote Post
JakubBab
post 10.03.2016, 09:59:15
Post #5





Grupa: Zarejestrowani
Postów: 96
Pomógł: 2
Dołączył: 13.07.2015

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


Cytat(Pyton_000 @ 10.03.2016, 08:07:22 ) *
To może pokaż kod który nie działa? Bo strasznie zamieszałeś.




Chodzi o $this->temp. W pierwszej funkcji przyjmuje on $this->giveCalendarNumber (ktory jest ekwiwalentem 7.03.2016) .RowTh() w ktorym znajduje sie $this->temp wyswietlam go <form> i dziala (czyli <form> wyswietla go jako string 7.03.2016). Jednak, gdy chce uzyc tej samej wlasciwosci ($this->temp) w innej ( w jakiejkolwiek) funkcji przyjmuje on wartosc NULL. Nie wiem dlaczego, ale obszedlem to tworzac inna funckje i przypisujac wartosc $this->giveCalendarNumber() do innej wlaciwosci.

W ramach przypomnienia: skad bierze sie $this->temp :

  1. protected $temp;
  2.  
  3. public function tableTh() {
  4.  
  5. for ($i = 0; $i <= 7; $i++) {
  6.  
  7. if ($i == 0) {
  8. $this->tableTh.='<td width="25px" style="background: #c0c0c0;">X</td>';
  9. } else if ($i == 1) {
  10. // -7 because it has to get the day commencing the week - monday
  11.  
  12. $this->tableTh .= '<td style="background: #c0c0c0;">' . $this->giveCalendarName() . "</br>" . $this->giveCalendarNumber(7,'null) . '</td>';
  13. $this->temp= $this->giveCalendarNumber(7,$i-7);
  14.  
  15. } else if ($i > 1) {
  16. // -7 because it has to get the day commencing the week - monday
  17. $this->tableTh .= '<td style="background: #c0c0c0;">' . $this->giveCalendarName($i - 1) . "</br>" . $this->giveCalendarNumber(7,$i-1) . '</td>';
  18. $this->temp= $this->giveCalendarNumber(7,$i-7);
  19.  
  20. }
  21. }
  22.  
  23. return $this->tableTh;
  24. }


Tutaj $this->temp dziala i wyswietla to co chce (7.03.2016).
  1. public function tableRow() {
  2.  
  3.  
  4. for ($i = 1; $i <= 8; $i++) {
  5. echo "<tr></tr>";
  6.  
  7. for ($j = 0; $j <= 7; $j++) {
  8.  
  9. if ($j == 0) {
  10. echo '<td width="30px" style="background: #c0c0c0;">' . $this->giveTime(9 + $i - 1) . '</br></td>';
  11. } else {
  12.  
  13. echo '<td><input type="checkbox" name="Reservation[' . $j . ']['.$this->temp.'][' . $i . ']" value="1"></br></td>';
  14. }
  15. }
  16. }
  17.  
  18. }




Przykladowa funkcja (w tej samej klasie ) ktorej to samo $this->temp zwraca wartosc null

  1. public function giveTemp(){
  2. $test = $this->temp;
  3. return $test;
  4. }
Go to the top of the page
+Quote Post
Pyton_000
post 10.03.2016, 10:02:42
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Ale wiesz o tym że musisz najpierw wywołać tableTh() a potem giveTemp() ?
Go to the top of the page
+Quote Post
JakubBab
post 10.03.2016, 10:15:08
Post #7





Grupa: Zarejestrowani
Postów: 96
Pomógł: 2
Dołączył: 13.07.2015

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


facepalmxd.gif

Skoro zaczalem watek to i go skoncze - chociaz wstyd....

Powod dla ktorego otrzymywalem wartosc null w kazdej innej funckcji to: (podpowiedz: patrz na zmienna do ktorej przypisany jest objekt)
(zmienilem widocznosc temp z protected na public co by sprwadzic):

Tu dziala
  1. $calendar = new My_calendar();
  2. echo '<table border="1" cellpadding ="1" cellspacing="0" style="text-align: right;">';
  3.  
  4. echo "<tr>";
  5. echo $calendar->tableTh();
  6. echo "</tr>";
  7. echo $calendar->tableRow();
  8. echo "</table>";
  9.  
  10.  
  11. echo $form->formEnd('ok');


Tu nie dziala

  1.  
  2. $test = new My_Calendar();
  3. $test->temp


Ten post edytował JakubBab 10.03.2016, 10:15:56
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 - 02:28