PHP: The Basics - Manual in Deutsh
PHP: The Basics - Manual in French
PHP: The Basics - Manual in Polish

You Are At PHP: The Basics - Manual Area


recent searches:
include functions , variable functions , post functions...




If you are new to PHP or just need to refresh your skills, this is the place to start. This series of tutorials will give you the basic knowledge you will need to create a simple PHP website.

PHP is a reflective programming language originally designed for producing dynamic web pages.[1] PHP is used mainly in server-side scripting, but can be used from a command line interface or in standalone graphical applications. Textual User Interfaces can also be created using ncurses.

PHP Manual
PrevChapter 19. Classes and Objects (PHP 5)Next

The Basics

class

Every class definition begins with the keyword class, followed by a class name, which can be any name that isn't a reserved word in PHP. Followed by a pair of curly braces, which contains the definition of the classes members and methods. A pseudo-variable, $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object). This is illustrated in the following examples:

Example 19-1. $this variable in object-oriented language

<?php
class A
{
    function
foo()
    {
        if (isset(
$this)) {
            echo
'$this is defined (';
            echo
get_class($this);
            echo
")\n";
        } else {
            echo
"\$this is not defined.\n";
        }
    }
}

class
B
{
    function
bar()
    {
        
A::foo();
    }
}

$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>

The above example will output:

$this is defined (a)
$this is not defined.
$this is defined (b)
$this is not defined.

Example 19-2. Simple Class definition

<?php
class SimpleClass
{
    
// member declaration
    
public $var = 'a default value';

    
// method declaration
    
public function displayVar() {
        echo
$this->var;
    }
}
?>

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

Example 19-3. Class members' default value

<?php
class SimpleClass
{
    
// invalid member declarations:
    
public $var1 = 'hello '.'world';
    
public $var2 = <<<EOD
hello world
EOD;
    
public $var3 = 1+2;
    
public $var4 = self::myStaticMethod();
    
public $var5 = $myVar;

    
// valid member declarations:
    
public $var6 = myConstant;
    
public $var7 = self::classConstant;
    
public $var8 = array(true, false);
    
    
}
?>

Note: There are some nice functions to handle classes and objects. You might want to take a look at the Class/Object Functions.

new

To create an instance of a class, a new object must be created and assigned to a variable. An object will always be assigned when creating a new object unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement).

Example 19-4. Creating an instance

<?php
$instance
= new SimpleClass();
?>

When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A copy of an already created object can be made by cloning it.

Example 19-5. Object Assignment

<?php
$assigned   
=  $instance;
$reference  =& $instance;

$instance->var = '$assigned will have this value';

$instance = null; // $instance and $reference become null

var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>

The above example will output:

NULL
NULL
object(SimpleClass)#1 (1) {
   ["var"]=>
     string(30) "$assigned will have this value"
}

extends

A class can inherit methods and members of another class by using the extends keyword in the declaration. It is not possible to extend multiple classes, a class can only inherit one base class.

The inherited methods and members can be overridden, unless the parent class has defined a method as final, by redeclaring them within the same name defined in the parent class. It is possible to access the overridden methods or members by referencing them with parent::

Example 19-6. Simple Class Inherintance

<?php
class ExtendClass extends SimpleClass
{
    
// Redefine the parent method
    
function displayVar()
    {
        echo
"Extending class\n";
        
parent::displayVar();
    }
}

$extended = new ExtendClass();
$extended->displayVar();
?>

The above example will output:

Extending class
a default value

Grzejniki
Wycieraczki
kredyt hipoteczny doradca finansowy wrocław
kredyt banki
Koszulki

Another Useful functions:


language.oop5.visibility | language.oop5.typehinting | language.oop5.static | language.oop5.reflection | language.oop5.patterns | language.oop5.paamayim-nekudotayim | language.oop5.overloading | language.oop5.object-comparison | language.oop5.magic | language.oop5.iterations | language.oop5.interfaces | language.oop5 | language.oop5.final | language.oop5.decon | language.oop5.constants | language.oop5.cloning | language.oop5.basic | language.oop5.autoload | language.oop5.abstract |


PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. After that, check out the online manual, and the example archive sites and some of the other resources available in the links section.