Wat is $this

$this is een methode om binnen de Class of extends van Classes te werken.

Voorbeeld #1: Binnen de class zelf

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
class File_General
{
    var
$m_sFilePath;
    var
$m_sFileName;

    // Function to check if a file exists's.
    function Check_If_Exist($p_sFilePath,$p_sFileName)
    {

    
        // Storing parameter data into $this (object of the class).
        $this->m_sFilePath = $p_sFilePath;
        $this->m_sFileName = $p_sFileName;

        // Executing function in this class.
        $this->Just_For_Demonstrating();
    }
    
    
    function
Just_For_Demonstrating()
    {

        // Normaly you dont use output to display information inside
        // a class, its just here to show you the results if you try
        // running this example for yourself.

        echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
    }
}




// Creating an instance of the File_Pictures class.
$my_general_file = new File_General();

// Using a function of the File_Pictures class
$my_general_file->Check_If_Exist('Map','foto.jpg');

?>


Zoals je ziet als de functie Check_If_Exist() wordt aangesproken met een aantal parameters op regel 35 dan zal deze in de class zelf de parameters opslaan in het object en dan een andere functie genaamd Just_For_Demonstrating() aanroepen die dan ter demonstratie de verkregen gegevens zal vertonen.




Voorbeeld #2: Vanuit de extends class

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
class File_General
{
    var
$m_sFilePath;
    var
$m_sFileName;

    // Function to check if a file exists's.
    function Check_If_Exist()
    {

        // Normaly you dont use output to display information inside
        // a class, its just here to show you the results if you try
        // running this example for yourself.

        echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
    }
}


Class File_Pictures extends File_General
{
    var
$m_sFilePath;
    var
$m_sFileName;
    
    // Function to check if the file is indeed a picture.
    function Check_If_Picture($p_sFilePath,$p_sFileName)
    {

            $this->m_sFilePath = $p_sFilePath;
            $this->m_sFileName = $p_sFileName;
            $this->Check_If_Exist();
    }
}


// Creating an instance of the File_Pictures class.
$my_picture_file = new File_Pictures();

// Using a function of the File_Pictures class
$my_picture_file->Check_If_Picture('Map','foto.jpg');

?>


Nu spreken we een methode aan vanuit de extended Class met $this->Check_If_Picture() die dan de parameters opslaat in het Object,
Vervolgens een functie $this->Check_If_Exist() aanspreekt die in de
extends Class File_General staat.

Opmerking:

Indien dezelfde functie in beide Classes voorkomt zal het aanspreken
van een functie via $this leiden tot onverwachte resultaten
als je daar geen rekening mee houdt.

In zon geval is het beter om concreet te zijn welke functie in welke class je wil aanspreken door gebruik te maken van een Scope resolution operator.

Zo ongewenste situatie staat in voorbeeld #3 van Sectie: Wat is Parent

« Lees de omschrijving en reacties

Inhoudsopgave

  1. Inleiding
  2. Wat is Classification
  3. Wat is een Instantiation
  4. Wat is Inheritance
  5. Wat is Parent
  6. Wat is $this
  7. Wat is Self
  8. Wat is een Constructor
  9. Wat is een Scope resolution operator

PHP tutorial opties

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.