П О Р Т А Л                            
С Е Т Е В Ы Х                          
П Р О Е К Т О В                        
  
Поиск по сайту:
                                                 
Главная

О проекте

Web-мастеру
     HTML & JavaScript
     SSI
     Perl
     PHP
     XML & XSLT
     Unix Shell

MySQL

Безопасность

Хостинг

Другое








Самое читаемое:

Учебник PHP - "Для Чайника".
Просмотров 3519 раз(а).

Иллюстрированный самоучитель по созданию сайтов.
Просмотров 6119 раз(а).

Учебник HTML.
Просмотров 3275 раз(а).

Руководство по PHP5.
Просмотров 5497 раз(а).

Хостинг через призму DNS.
Просмотров 4144 раз(а).

Подборка текстов стандартных документов.
Просмотров 55772 раз(а).

Учебник PHP - Самоучитель
Просмотров 3089 раз(а).

Документация на MySQL (учебник & справочное руководство)
Просмотров 5688 раз(а).

Внешние атаки...
Просмотров 3839 раз(а).

Учебник PHP.
Просмотров 2825 раз(а).

SSI в примерах.
Просмотров 37462 раз(а).



 
 
| Добавить в избранное | Сделать стартовой | Помощь





Руководство по PHP
Пред. Глава 19. Классы и объекты (PHP 5) След.

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, of 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 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();
?>

Результат выполнения данного примера:

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

Пример 19-2. Simple Class definition

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

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

new

To create an instance of an object, 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.

Пример 19-3. Creating an instance

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

When assigning an already created instance of an object 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 new instance of an already created object can be made by cloning it.

Пример 19-4. 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);
?>

Результат выполнения данного примера:

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 overrided method or members by referencing them with parent::

Пример 19-5. 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();
?>

Результат выполнения данного примера:

Extending class
a default value

Пред. Начало След.
Классы и объекты (PHP 5) Уровень выше Autoloading Objects


Если Вы не нашли что искали, то рекомендую воспользоваться поиском по сайту:
 





Copyright © 2005-2016 Project.Net.Ru