barak Fri Jan 25 19:54:53 2002 EDT
Added files:
/phpdoc/he/language oop.xml
Log:
initial of translating this file
Index: phpdoc/he/language/oop.xml
+++ phpdoc/he/language/oop.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<chapter id="language.oop">
<title>Classes and Objects</title>
<sect1 id="keyword.class">
<title><literal>������</literal></title>
<para>
����� ��� ���� �� ������ ��������� ������� ��
������ ���. ����� ������ ��� ����� ������ ���:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class Cart
{
var $items; // ����� ����� �� ������ ��� $items
// Add $num articles of $artnr to the cart
function add_item ($artnr, $num)
{
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
function remove_item ($artnr, $num)
{
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} else {
return false;
}
}
}
?>
]]>
</programlisting>
</informalexample>
</para>
<para>
������ ��"�, ������ ����� ���� Cart, ������� ����� ������� �������
����� ��� (Cart), ����� �������� ������ (add) ������ (remove) ��
������ ���� �����.
</para>
<caution>
<simpara>
������ ��� ����� � PHP4 !.
</simpara>
<simpara>
����� <literal>stdClass</literal> ��� ���� ����� ������ ������
����� �� ��� ZEND. ���, ��� ������ �����
<literal>stdClass</literal> � PHP.
</simpara>
<simpara>
�� �������� <literal>__sleep</literal> ��� ��������
<literal>__wakeup</literal> ����� ����� ����� � - php.
��� �� ���� ������ ��� ���� ���� ���� ��������� ��"� ������ �����
��� �� ��� ���� ������ ��� ������� ������� (������ �����) ��� �� �����.
��� ����� ������ ����� ����.
</simpara>
<simpara>
�� �������� � PHP �������� (����� ����� ���� ���� �����) ������� �� __.
����� ���� �� ����� ����� ������� � PHP �� �� ������ �
__ ��� �� ��� ���� ���� ���� ��������� "������".
</simpara>
</caution>
<note>
<simpara>
� PHP 4, ������ ����� ����� ������� ����� <literal>var</literal>
����� �� �� ������. ��� ����� ������, ��� �� ��� ���� ������,
��� ���� ������ �������� ������ �������� �� ��� PHP
���� ���� ������� ��� �� ��������.
������� ���� ����� "������� �����" (constructor) (��� ����).
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
/* ������ ����, ����� ����� ����� ���� ������ ������ ���� ! */
class Cart
{
var $todays_date = date("Y-m-d");
var $name = $firstname;
var $owner = 'Fred ' . 'Jones';
var $items = array("VCR", "TV");
}
/* ��� �� ���� ������: */
class Cart
{
var $todays_date;
var $name;
var $owner;
var $items;
function Cart() // ����� �� ������
{
$this->todays_date = date("Y-m-d");
$this->name = $GLOBALS['firstname'];
/* etc. . . */
}
}
]]>
</programlisting>
</informalexample>
</note>
<para>
������ �� ������, �����, �� ���� ������ ������ ������� (����������)
������ �� ������� ����� ������ ��� ����� ��������
<literal>new</literal>.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$cart = new Cart;
$cart->add_item("10", 1);
$another_cart = new Cart;
$another_cart->add_item("0815", 3);
]]>
</programlisting>
</informalexample>
<para>
������ ��"� ���� �� ���������� $cart � $another_cart, ����� ���������
������ ������� Cart. �������� add_item() �� �������� - $cart
����� �� ��� ������ ���� ��� �� ��� ��' 10 ���� �����.
3 ������ ��� ��' 0815 ����� ��������
$another_cart.
</para>
<para>
�� � $cart ��� � $another_cart, �� �� ��������� add_item(),
remove_item() ������ �� ������ ($items) . ��� ��
�������� ������� ������. ���� ����� �� ��������� ������
������� ������ ������ �����. ���� ����� �����
������ ����� �� ��� ���� README.TXT ����� �������, ��
��� �� ������ ������ ����. ��� ��� ������� �����,
������ ������ �� ������ ���� �� ���
������ ���� �����, �� �� ������ ������ ���,
�� ���� ��� �� ��� ���� �� �������� ���� �����
�����, ������� �� php: "��� �����" �� �������� ���� �����
���� �������, ������� ��� ���� ��� ->.
���, ������ $cart->items � $another_cart->items
�� ���� �� ��� ������ �����. �� ���� �� ������ ������� ����
$cart->items, ��� $cart->$items, ���� ���,
���� ����� � PHP �� �� ���� $ ���.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
// correct, single $
$cart->items = array("10" => 1);
// ����, ���� � $cart->$items ���� ����� $cart->""
$cart->$items = array("10" => 1);
//����, ��� ���� ����� �� ���� ��� ���� �������
//$cart->$myvar ��� ����� $cart->items
$myvar = 'items';
$cart->$myvar = array("10" => 1);
]]>
</programlisting>
</informalexample>
<para>
�� ����� ������, ��� ������ ���� ���� ���� ��������� ������
������� ����� �����: ���� ������� Cart �����,
�� ��� ���� ������ �������� $cart �� ��������
$another_cart ����� ����. ���, ���� ����� ����� $cart->items ����
������ Cart ����. ����� ���, ��� ����� ���� ����� ���� ���� ���������
�������� �� ������� ���� ������ ����, ���� ������ �
������� - ����� (����� �����) $this ����� ����� � '�� ����' ��
'������� �����'. �����, '$this->items[$artnr] += $num' ����
����� � '���� $num ����� $artnr �� ���� ������ �� ����'
�� '���� $num ����� $artnr �� ���� ������
����� ���� �������� ������'.
</para>
</sect1>
<sect1 id="keyword.extends">
<title><literal>������</literal></title>
<para>
����� ������, ������ ������� �� �� ������ ���������
����� ������� ������� ��������� �����. �����, �� ���� ���
������ ����� ����� ������ ���� ���� ��� ���������� ���
������� �� ����� ��� ����� �� ��� ����������� ������.
����� ����, ������ ������ �����
������ �� ������ �����. ������ ������� �� ������
�� �� �� ������� ���������� �� ������ ������� (�� ����
'�����' ����� ������ ��� ��� �� ��) ��� �� �� ���� �����
�� (�������� ������� ������) �� ���� �����
���� ����� �� ����� �� ������� ����� ������.
����� ������, ����� ����� �� ����
�� ����� ��� ����,���� ����, ������ �����
�� �����. ������ ������� ��� ����� ����� ������ 'extends'.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
class Named_Cart extends Cart
{
var $owner;
function set_owner ($name)
{
$this->owner = $name;
}
}
]]>
</programlisting>
</informalexample>
<para>
������ ��"� ������ ����� Named_Cart ��� �� �� �� ��������� ��������
�� Cart ����� ������ $owner ��������� �����
��� set_owner(). ������ �� ����� ���� ������, ��� ���� ��
����� ����� ������ �� ������ �� ��� �����. ����� ���� ������
�������� ������� �� �����.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
$ncart = new Named_Cart; // ����� �������
$ncart->set_owner("kris"); // ����� �� �� ����� ����� ����
print $ncart->owner; // ���� �� ��� ������ �� �����
$ncart->add_item("10", 1); // ����� �������� ������� ���� cart
]]>
</programlisting>
</informalexample>
</sect1>
<sect1 id="language.oop.constructor">
<title><literal>Constructors</literal></title>
<caution>
<simpara>
� PHP 3 � PHP 4 ��������� ������� ������� ����� ����. ��������
�� PHP 4 ����� �����.
</simpara>
</caution>
<para>
����� �� �������� ������ �������� ��������
�������� ���� ��� �� ������ (��� ����� ������� ���) ��� ����� �����
<literal>new</literal>. � PHP 3,
�������� ������� ������� ���� ������ ��� ���� ��� ������.
� PHP 4, �������� ������ �������, ����
����� ���� �� �� ����� ��� ������� ����� - �����
��� ������ ���, ��� ����� (��� ����).
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
// ���� � PHP 3 ��� � PHP 4.
class Auto_Cart extends Cart
{
function Auto_Cart()
{
$this->add_item ("10", 1);
}
}
]]>
</programlisting>
</informalexample>
<para>
������ ������ ����� Auto_Cart ���� Cart ������ �� ����
������ �� ����� �� ��� ��� �� ���� ������ "10"
��� ��� ������� ���� ��� �� Auto_Cart ����� "new". ������� ������
����� ���� ��������� ��� �����������, �� �����
��� ����� �������� ������. �� ��� ����� ���� ������ ��������
��� �������, �� �������� �������� ����� ����� ����� ���������� �� ���
����� ���� ����� ����.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
// ���� �� � PHP 3 ��� � PHP 4.
class Constructor_Cart extends Cart
{
function Constructor_Cart($item = "10", $num = 1)
{
$this->add_item ($item, $num);
}
}
// Shop the same old boring stuff.
$default_cart = new Constructor_Cart;
// Shop for real...
$different_cart = new Constructor_Cart("20", 17);
]]>
</programlisting>
</informalexample>
<caution>
<simpara>
� PHP 3, ������� ������ ������ �� ��� ������.
������ ���� ����� ����� �������
�� ��� �������� ���� ������.
</simpara>
</caution>
<informalexample>
<programlisting role="php">
<![CDATA[
class A
{
function A()
{
echo "I am the constructor of A.<br>\n";
}
}
class B extends A
{
function C()
{
echo "I am a regular function.<br>\n";
}
}
// no constructor is being called in PHP 3.
$b = new B;
]]>
</programlisting>
</informalexample>
<para>
� PHP 3, ��� ������� ����� �� ���� ������ ��"�.
���� � PHP 3 ���: '����� A ��� �������� �� ���� ��
�� ������.'. ��� �� ������ ��� B, ���� ���
������� ������ B() ������ B. ��� ���� �� ����.
</para>
<para>
����� ��� ���� � PHP 4 �� ��� ����� ��� ���: �� ������ ���
������� �����, �������� ����� �� ����� �����
�����, �� ��� �����. ������ ���, � PHP4 ��� ����� �������� ����� ����:
'I am the constructor of A.<br>'.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
class A
{
function A()
{
echo "I am the constructor of A.<br>\n";
}
function B()
{
echo "I am a regular function named B in class A.<br>\n";
echo "I am not a constructor in A.<br>\n";
}
}
class B extends A
{
function C()
{
echo "I am a regular function.<br>\n";
}
}
// This will call B() as a constructor.
$b = new B;
]]>
</programlisting>
</informalexample>
<para>
� PHP 3, �������� B() ������� A ���� �����
������ ������ B, ����� ��� ��� "������ ������".
���� � PHP 3 ���: '����� A ��� �������� ���� ����
�� �� ������.'.� PHP 3 �� ����� �� ��������
������ ������ B, �� �� ��� ����� ������ A .
</para>
<para>
�� ����� �� ���� � PHP 4 �� ��� ����� ���� ���: '�������� ������ A
��� ������� ���� ��� �� �� ������ ���� ������ �����.
'��� � PHP 4, ������ B ���� �� �� ������ �� ����
����� �������� ������ �� ����� �����, ������ ����� �� ����
'I am the constructor of A.<br>'.
</para>
<caution>
<simpara>
�� PHP 3 ��� PHP 4 ������ ����� �� ����� �����
�������� ����� �� ����� �����. �� ������� ������
����� �������� ������ �� ����� ����� ����� ���.
</simpara>
</caution>
<note>
<simpara>
��� "������� �����" (destructors) � PHP 3 �� PHP 4. ��� ���� ������ �
<function>register_shutdown_function</function> ����� ���
����� �� ��� �������� �� "������� �����" (����� ������).
</simpara>
</note>
<para>
"������� �����" ��� ������� ������ ������� ���� "�������" �������,
�� �� ��� ����� � <function>unset</function>
��� ���� �� ��� ����� ���� ����� �� ��������. ��� �����, ��� ��������
���� �� � PHP.
</para>
</sect1>
<sect1 id="keyword.paamayim-nekudotayim"><!-- :-) -->
<title><literal>::</literal></title>
<caution>
<simpara>
���� ��� ��� � PHP 4 ����.
</simpara>
</caution>
<para>
������ �� ������ ������� � functions and variables
in base classes or to refer to functions in classes that
have not yet any instances. The :: operator is being used
for this.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
class A
{
function example()
{
echo "I am the original function A::example().<br>\n";
}
}
class B extends A
{
function example()
{
echo "I am the redefined function B::example().<br>\n";
A::example();
}
}
// there is no object of class A.
// this will print
// I am the original function A::example().<br>
A::example();
// create an object of class B.
$b = new B;
// this will print
// I am the redefined function B::example().<br>
// I am the original function A::example().<br>
$b->example();
]]>
</programlisting>
</informalexample>
<para>
The above example calls the function example() in
class A, but there is no object of class A, so that
we cannot write $a->example() or similar. Instead we
call example() as a 'class function', that is, as a
function of the class itself, not any object of that
class.
</para>
<para>
There are class functions, but there are no class variables.
In fact, there is no object at all at the time of the call.
Thus, a class function may not use any object variables (but
it can use local and global variables), and it may no use
$this at all.
</para>
<para>
In the above example, class B redefines the function example().
The original definition in class A is shadowed
and no longer available, unless you are refering specifically
to the implementation of example() in class A using the
::-operator. Write A::example() to do this (in fact, you
should be writing parent::example(), as shown in the next
section).
</para>
<para>
In this context, there is a current object and it may
have object variables. Thus, when used from WITHIN an
object function, you may use $this and object variables.
</para>
</sect1>
<sect1 id="keyword.parent">
<title><literal>parent</literal></title>
<para>
You may find yourself writing code that refers to
variables and functions in base classes. This is
particularly true if your derived class is a refinement
or specialisation of code in your base class.
</para>
<para>
Instead of using the literal name of the base class in your
code, you should be using the special name
<literal>parent</literal>, which refers to the name of your
base class as given in the <literal>extends</literal>
declation of your class. By doing this, you avoid using the
name of your base class in more than one place. Should
your inheritance tree change during implementation, the
change is easily made by simply changing the
<literal>extends</literal> declaration of your class.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
class A
{
function example()
{
echo "I am A::example() and provide basic functionality.<br>\n";
}
}
class B extends A
{
function example()
{
echo "I am B::example() and provide additional functionality.<br>\n";
parent::example();
}
}
$b = new B;
// This will call B::example(), which will in turn call A::example().
$b->example();
]]>
</programlisting>
</informalexample>
</sect1>
<sect1 id="language.oop.serialization">
<title>Serializing objects - objects in sessions</title>
<note>
<simpara>
In PHP 3, objects will lose their class association
throughout the process of serialization and unserialization.
The resulting variable is of type object, but has no class
and no methods, thus it is pretty useless (it has become
just like an array with a funny syntax).
</simpara>
</note>
<caution>
<simpara>
The following information is valid for PHP 4 only.
</simpara>
</caution>
<para>
<function>serialize</function> returns a string containing a
byte-stream representation of any value that can be stored in
PHP. <function>unserialize</function> can use this string to
recreate the original variable values. Using serialize to
save an object will save all variables in an object. The
functions in an object will not be saved, only the name of
the class.
</para>
<para>
In order to be able to <function>unserialize</function> an
object, the class of that object needs to be defined. That
is, if you have an object $a of class A on page1.php and
serialize this, you'll get a string that refers to class A
and contains all values of variabled contained in $a. If
you want to be able to unserialize this on page2.php,
recreating $a of class A, the definition of class A must
be present in page2.php. This can be done for example
by storing the class defintion of class A in an include
file and including this file in both page1.php and page2.php.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
classa.inc:
class A
{
var $one = 1;
function show_one()
{
echo $this->one;
}
}
page1.php:
include("classa.inc");
$a = new A;
$s = serialize($a);
// store $s somewhere where page2.php can find it.
$fp = fopen("store", "w");
fputs($fp, $s);
fclose($fp);
page2.php:
// this is needed for the unserialize to work properly.
include("classa.inc");
$s = implode("", @file("store"));
$a = unserialize($s);
// now use the function show_one() of the $a object.
$a->show_one();
]]>
</programlisting>
</informalexample>
<para>
If you are using sessions and use <function>session_register</function>
to register objects, these objects are serialized automatically
at the end of each PHP page, and are unserialized automatically on
each of the following pages. This basically means that these objects
can show up on any of your pages once they become part of your
session.
</para>
<para>
It is strongly recommended that you include the class
definitions of all such registered objects on all of your
pages, even if you do not actually use these classes on all
of your pages. If you don't and an object is being
unserialized without its class definition being present, it
will lose its class association and become an object of class
<literal>stdClass</literal> without any functions available
at all, that is, it will become quite useless.
</para>
<para>
So if in the example above $a became part of a session by
running <literal>session_register("a")</literal>, you should
include the file <literal>classa.inc</literal> on all of your
pages, not only page1.php and page2.php.
</para>
</sect1>
<sect1 id="language.oop.magic-functions">
<title>The magic functions <literal>__sleep</literal> and
<literal>__wakeup</literal></title>
<para>
<function>serialize</function> checks if your class has a function with
the magic name <literal>__sleep</literal>. If so, that function is
being run prior to any serialization. It can clean up the object
and is supposed to return an array with the names of all variables
of that object that should be serialized.
</para>
<para>
The intended use of <literal>__sleep</literal> is to close any
database connections that object may have, committing pending
data or perform similar cleanup tasks. Also, the function is
useful if you have very large objects which need not be
saved completely.
</para>
<para>
Conversely, <function>unserialize</function> checks for the
presence of a function with the magic name
<literal>__wakeup</literal>. If present, this function can
reconstruct any resources that object may have.
</para>
<para>
The intended use of <literal>__wakeup</literal> is to
reestablish any database connections that may have been lost
during serialization and perform other reinitialization
tasks.
</para>
</sect1>
<sect1 id="language.oop.newref">
<title>References inside the constructor</title>
<para>
Creating references within the constructor can lead to confusing
results. This tutorial-like section helps you to avoid problems.
<informalexample>
<programlisting role="php">
<![CDATA[
class Foo
{
function Foo($name)
{
// create a reference inside the global array $globalref
global $globalref;
$globalref[] = &$this;
// set name to passed value
$this->setName($name);
// and put it out
$this->echoName();
}
function echoName()
{
echo "<br>",$this->name;
}
function setName($name)
{
$this->name = $name;
}
}
]]>
</programlisting>
</informalexample>
</para>
<para>
Let us check out if there is a difference between
<varname>$bar1</varname> which has been created using
the copy <literal>=</literal> operator and
<varname>$bar2</varname> which has been created using
the reference <literal>=&</literal> operator...
<informalexample>
<programlisting role="php">
<![CDATA[
$bar1 = new Foo('set in constructor');
$bar1->echoName();
$globalref[0]->echoName();
/* output:
set in constructor
set in constructor
set in constructor */
$bar2 =& new Foo('set in constructor');
$bar2->echoName();
$globalref[1]->echoName();
/* output:
set in constructor
set in constructor
set in constructor */
]]>
</programlisting>
</informalexample>
</para>
<para>
Apparently there is no difference, but in fact there is a
very significant one: <varname>$bar1</varname> and
<varname>$globalref[0]</varname> are _NOT_ referenced, they
are NOT the same variable. This is because "new" does not
return a reference by default, instead it returns a copy.
<note>
<simpara>
There is no performance loss (since PHP 4 and up use reference
counting) returning copies instead of references. On the
contrary it is most often better to simply work with copies
instead of references, because creating references takes some
time where creating copies virtually takes no time (unless none
of them is a large array or object and one of them gets changed
and the other(s) one(s) subsequently, then it would be wise to
use references to change them all concurrently).
</simpara>
</note>
To prove what is written above let us watch the code below.
<informalexample>
<programlisting role="php">
<![CDATA[
// now we will change the name. what do you expect?
// you could expect that both $bar1 and $globalref[0] change their names...
$bar1->setName('set from outside');
// as mentioned before this is not the case.
$bar1->echoName();
$globalref[0]->echoName();
/* output:
set from outside
set in constructor */
// let us see what is different with $bar2 and $globalref[1]
$bar2->setName('set from outside');
// luckily they are not only equal, they are the same variable
// thus $bar2->name and $globalref[1]->name are the same too
$bar2->echoName();
$globalref[1]->echoName();
/* output:
set from outside
set from outside */
]]>
</programlisting>
</informalexample>
</para>
<para>
Another final example, try to understand it.
<informalexample>
<programlisting role="php">
<![CDATA[
class A
{
function A($i)
{
$this->value = $i;
// try to figure out why we do not need a reference here
$this->b = new B($this);
}
function createRef()
{
$this->c = new B($this);
}
function echoValue()
{
echo "<br>","class ",get_class($this),': ',$this->value;
}
}
class B
{
function B(&$a)
{
$this->a = &$a;
}
function echoValue()
{
echo "<br>","class ",get_class($this),': ',$this->a->value;
}
}
// try to undestand why using a simple copy here would yield
// in an undesired result in the *-marked line
$a =& new A(10);
$a->createRef();
$a->echoValue();
$a->b->echoValue();
$a->c->echoValue();
$a->value = 11;
$a->echoValue();
$a->b->echoValue(); // *
$a->c->echoValue();
/*
output:
class A: 10
class B: 10
class B: 10
class A: 11
class B: 11
class B: 11
*/
]]>
</programlisting>
</informalexample>
</para>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->