Wat is Self

Self:: is niet een van de Scope resolution operators die je kan gebruiken in Php4 maar wil ik toch even dit onderwerp aanhalen zodat je weet wat dit inhoudt en hoe het zelfde effect kan worden bereikt.

We moeten een alternatieve wijze gaan gebruiken en dat is gebruik maken van een Scope resolution operators waarbij we de naam van de Class zelf gebruiken.

Voorbeeld:

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
<?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.
        File_General::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');

?>


Op deze manier zal File_General::Just_For_Demonstrating() de functie alleen binnen de Class zelf aanspreken ongeacht of er nog meerdere classes zijn die middels extends in elkaars verlenging liggen.

« 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.