|
|
 |
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:
Example 19-1. Simple Class definition |
<?php
class SimpleClass
{
public $var = 'a default value';
public function displayVar() {
echo $this->var;
}
}
?>
|
|
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.
Example 19-2. 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.
Example 19-3. Object Assignment |
<?php
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = 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"
} |
|
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::
Example 19-4. Simple Class Inherintance |
<?php
class ExtendClass extends SimpleClass
{
function displayVar()
{
echo "Extending class\n";
parent::displayVar();
}
}
$extended = new ExtendClass();
$extended->displayVar();
?>
|
The above example will output: Extending class
a default value |
|
User Contributed Notes
The Basics
tobias_demuth at web dot de
04-May-2005 02:14
Looks like this doesn't work:
<?php
class Bar {
}
class Foo {
private $bar;
function __construct($bar = new Bar()) {
$this->bar = $bar;
}
}
?>
This breaks with "Unexpected T_NEW in path/to/file anylinenumber".
I would say, this is the same effect as in my second note on this page...
tobias_demuth at web dot de
04-May-2005 04:21
Not that it is not possible to access a with 'new' newly constructed object without assigning it to a variable. This works:
<?php
class Foo {
function __construct() {
}
function printHello() {
echo "Hello!\n";
}
}
class Working {
function __construct() {
}
function returnObject() {
return new Foo();
}
}
$working = new Working();
$working->returnObject()->printHello();
?>
In the above example, the method returnObject() constructs a new Foo()-instance and returns it. Because PHP5 is capable to dereference it, I can use multiple method-calls in one line.
Now to the point: This works _not_:
<?php
class Foo {
function __construct() {
}
function printHello() {
echo "Hello!\n";
}
}
class Working {
function __construct() {
}
function returnObject() {
return new Foo();
}
}
new Working()->returnObject()->printHello();
?>
Mainly the same code - I just did not assign the newly constructed Working-instance to a variable. This will break with an error.
rasmus at nospaam dot flajm dot com
05-Apr-2005 05:22
A very good (in my opinion) change in PHP5 is that objects are always passed by reference. In PHP4 they where passed by value, which cased a copy to be made for every call. (as long as you didn't use "&" of cause)
This example illustrates pass-by-reference, as the name changes in the same object, passed around two different ways:
<?php
class Mother {
private $obj = null;
public function setChild($o) {
$this->obj = $o;
}
public function getChild() {
return $this->obj;
}
}
class Child {
private $name = 'noname';
public function setName($string) {
$this->name = $string;
}
public function getName() {
return $this->name;
}
}
$mom = new Mother();
$frasse = new Child();
$frasse->setName('Johan');
$mom->setChild($frasse);
$frasse->setName('Frans');
print $frasse->getName() . "\n";
print $mom->getChild()->getName() . "\n";
?>
This will print:
Frans
Frans
In PHP4, this would print:
Frans
Johan
graced at monroe dot NOSPAM dot wednet dot edu
29-Mar-2005 12:46
In reponse to jerry's comment: "If you instantiate a class and assign it to a variable over and over again, php will start saving instances #1 and #2 in memory but will stop at these"...
This is because after assigning the new object to the variable, there is no longer anything referencing the old object so it is destroyed. If there WAS another reference, say you assigned another variable to the first variable before setting the first to a new class, it'd be preserved.
In reality, this really makes no difference to the programmer, just thought an explanation was in order.
<?php
$obj1 = new stdclass();
var_dump($obj1); $obj1 = new stdclass();
var_dump($obj1); $obj1 = new stdclass(); var_dump($obj1);
$obj2 = $obj1; $obj1 = new stdclass(); var_dump($obj1);
$obj1 = new stdclass(); ?>
tigr at mail15 dot com
01-Mar-2005 06:08
Objects are not being passed by reference as varables do. Let me try to explain:
Variable passing by reference means that two variables are being binded together, so that changing one variable leads to changes in the other. In fact, there is only one variable.
Object passing by reference is a bit different. It means that not the object itself is being passed (that would lead to copying it and all evil), but only reference to the object is being passed. Now, both VARIABLES point to the same object, BUT they DO NOT point to each other. There are TWO DIFFERENT variables. This means that if you change one VARIABLE, second one would still point to the same object.
So, adding reference operator still has some sense. Here is an example:
<?php
class sampleClass {
public $id;
public function __construct($id) { $this->id=$id; }
}
$object1 = new sampleClass(1);
$object2 = $object1;
echo $object1->id; echo $object2->id; $object2 = new sampleClass(2);
echo $object1->id; echo $object2->id; $object3 = &$object1; $object3 = new sampleClass(3);
echo $object1->id; echo $object2->id; echo $object3->id; ?>
jerry
12-Jan-2005 12:41
If you instantiate a class and assign it to a variable over and over again, php will start saving instances #1 and #2 in memory but will stop at these, and will not save more than those instances(ie. #3, #4, #5, #6...). Instead it will toggle between #1 and #2.
<?php
$obj1 = new stdclass();
var_dump($obj1);
$obj1 = new stdclass();
var_dump($obj1);
$obj1 = new stdclass();
var_dump($obj1);
$obj1 = new stdclass();
var_dump($obj1);
$obj1 = new stdclass();
var_dump($obj1);
$obj1 = new stdclass();
var_dump($obj1);
$obj1 = new stdclass();
var_dump($obj1);
?>
Richard (php at tsg1985 dot com)
12-Dec-2004 10:36
"Actually, the new variable will access a COPY of the instance and not the SAME instance. This is shown by the example: var_dump($assigned) was not NULLified because a COPY of the instance was assigned to $assigned..."
This is actually incorrect. However, it is explained really poorly in the article.
$instance = new SimpleClass();
This creates the new SimpleClass Object location in the memory. (This spot in memory stores all of the variable and the code to execute.)Then, it points the variable $instance to that location in the memory.
$assigned =$instance;
This takes the location that $instance is pointed to and points the variable $assigned to it as well.
$reference =& $instance;
This line of code points $refrence to the memory spot which contains the location of the SimpleClass object, not the object!
$instance->var = '$assigned will have this value';
This changes the values stored in SimpleClass Object's memory. Since $assigned points to this location as well, $assinged>var is the same value.
$instance = null;
This tells instance to point at nothing. So, it breaks its link to the SimpleClass object. $refrence points to $instance still, but since $instance points to nothing $refrence will also point to nothing.
I know this is kind of tough to understand, so I made an animated gif showing the steps.
http://www.prism.gatech.edu/~gtg624r/Code_Explenation.gif
| |