|
|
 |
CapÃtulo 19. Clases y Objetos (PHP 5)
En PHP 5 hay un nuevo modelo de Objetos. El manejo de PHP de objetos
ha sido reescrito por completo, permitiendo un mejor desempeño
y mas caracterÃsticas.
add a note
User Contributed Notes
Clases y Objetos (PHP 5)
seva-php at zend dot com
08-Aug-2006 01:35
Semi-proxy-patterned multi inheritance simulation example:
<?php
interface INamed
{
public function getName();
}
interface ISon
{
public function getFather();
}
interface IFather
{
public function getSons();
}
abstract class Named implements INamed
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function __destruct()
{}
public function getName()
{
return $this->name;
}
}
abstract class Son implements ISon
{
protected $father;
public function getFather()
{
return $this->father;
}
public function __construct(IFather $father = null)
{
$this->father = $father;
}
public function __destruct()
{}
}
abstract class Father implements IFather
{
protected $sons = array();
public function getSons()
{
return $this->sons;
}
public function __construct($sons = array())
{
$this->sons = $sons;
}
public function __destruct()
{}
}
class Man implements INamed, ISon, IFather
{
public function __construct($name, IFather $father = null, $sons = array())
{
Named::__construct($name);
Son::__construct($father);
Father::__construct($sons);
}
public function __destruct()
{
Named::__destruct();
Son::__destruct();
Father::__destruct();
}
public function getFather()
{
return Son::getFather();
}
public function getSons()
{
return Father::getSons();
}
public function getName()
{
return Named::getName();
}
}
$siarhej = new Man('Siaroh', new Man('Adas'), array(new Man('Seva')));
var_dump($siarhej->getName());
var_dump($siarhej->getFather()->getName());
$siarhejSons = $siarhej->getSons();
var_dump($siarhejSons[0]->getName());
?>
markfarquaad at gmail dot com
04-Aug-2006 02:23
Variable Object Attributes instead of variable variables:
Actually I figured this out on PHP 4.3, but I'm sure it works on the current PHP 5+:
If you like using variable variables and would like to use the same functionality with attributes of class objects, here's how you can can get a specific attribute of a specific class with variable OBJECTS:
Let's say this is our class:
class Car
{
var $brand;
var $color;
var $year;
function Car()
{
etc...
}
}
Let's say we have an object of Class 'Car', my own car, where $brand = 'Ferrari', $color='red', etc.:
$mycar = new Car();
And another 'Car' object, your car, where $brand='Porsche', $color='black', etc.:
$yourcar = new Car();
Now we need a uniform code snippet to access the color attribute of anyone's 'Car', but we would like to change the object dynamically outside the code delivering the color attribute. Here's how:
The variable or dynamic part is the object (the car owner):
$objectName = 'mycar';
or
$objectName = 'yourcar';
etc...
The static reusable code to deliver the color of any object dynamically is:
$objectAttributeName = 'color';
$objectAttribute = ${$objectName}->$objectAttributeName;
Now:
echo $mycar->color;
will deliver exactly the same as:
echo $objectAttribute
THe difference is, with the dynamic version, you do not have to know $objectName in the static code part, while in the non-variable version you must always specify the object name $mycar, $yourcar, etc to access the obeject's attributes.
I hope this helps anyone who likes reusing code like I do.
Mark
01-May-2006 06:06
Members can be added to instances on the fly.
Simply use
$apple= new fruit();
$pear=new fruit();
$apple->color='red';
$pear->smell='sweet';
and $apple only will contain a member (field) color, but $pear only will contain a field smell.
It is not clear however whether members an be added to the class at large on the fly.
25-Apr-2006 07:53
<?php
if (class_exists("cl_test")) return;
echo "ppppp";
class cl_test{
function cl_test(){
echo "quirky...";
}
} ?>
although the logic is clear php treats the above code differently. PHP translates it to:
class cl_test{
function cl_test(){
echo "quirky...";
}
} // class definition ends
if (class_exists("cl_test")) return;
echo "ppppp";
If you must have the previous logic to work you are forced to have the following code:
if (!class_exists("cl_test")) {
echo "ppppp";
class cl_test{
function cl_test(){
echo "quirky...";
}
} // class definition ends
}
epeterson5 at student dot gsu dot edu
07-Oct-2005 10:46
This isn't explicity noted anywhere, I don't think.
If you use a variable after the "->" part of a class attribute call, it will parse the variable as a string to assign/compare that class attribute:
<?php
class User {
var $test;
var $test2;
}
$user1 = new User();
$user2 = new User();
$user1->test = "user1's test";
$user1->test2 = "user1's test2";
$user2->test = "user2's test";
$user2->test2 = "user2's test2";
print_r($user1); print_r($user2);
foreach($user1 as $key => $value)
$user2->$key = $value;
print_r($user1); print_r($user2);
?>
Which outputs:
User Object: user1
[test] => user1's test
[test2] => user1's test2
User Object: user2
[test] => user2's test
[test2] => user2's test2
User Object: user1
[test] => user1's test
[test2] => user1's test2
User Object: user2
[test] => user1's test
[test2] => user1's test2
This is particularly useful when you want to assign a different instance of the same class to $this- I have a standard method that I call when I need to do that:
<?php
function assignToThis($new) {
foreach($new as $key => $value)
$this->$key=$value;
}
?>
turgut85 at hotmail dot com
29-Jun-2005 06:04
<?PHP
class Data {
public $Name;
public $Age;
public $Address;
public function __construct($Name,$Age,$Address) {
$this->Name = $Name;
$this->Age = $Age;
$this->Address = $Address;
}
public function __destruct() {
echo "Default Constructor...\n";
}
}
$ArrayList = array();
$ArrayList[0] = new Data("John McDonald","65","Address");
$ArrayList[1] = new Data("Turgut Z. Yesilyurt","30","NJ, USA");
$ArrayList[2] = new Data("Maria ","25","NJ, USA");
print_r($ArrayList);
$obj1 = $ArrayList[1];
echo "Name : ".$obj1->Name."\n";
echo "Age : ".$obj1->Age."\n";
echo "Address : ".$obj1->Address."\n";
interface ShowMe {
public function ShowName();
}
class ShowData implements ShowMe {
public $obj;
public function __construct($Obj) {
$this->obj = $Obj;
$this->ShowName();
}
public function ShowName() {
echo "Age : ".$this->obj->Age."\n";
}
}
echo "\nDisplay Single Data =========================\n\n";
new ShowData($ArrayList[1]);
echo "\n=============================================\n";
?>
Array
(
[0] => Data Object
(
[Name] => John McDonald
[Age] => 65
[Address] => Address
)
[1] => Data Object
(
[Name] => Turgut Z. Yesilyurt
[Age] => 30
[Address] => NJ, USA
)
[2] => Data Object
(
[Name] => Maria
[Age] => 25
[Address] => NJ, USA
)
)
Name : Turgut Z. Yesilyurt
Age : 30
Address : NJ, USA
Display Single Data =========================
Age : 30
=============================================
Default Constructor...
Default Constructor...
Default Constructor...
zabmilenko at hotmail dot com
27-Jun-2005 12:27
Dynamic instantiation trick:
<?php
class CITY
{
private $population;
public function __construct($cityname)
{
}
public function population($demographic = 'all')
{
return $this->population[$demographic];
}
}
class COUNTRY
{
private $code = null;
private $cities = array();
public function __construct($code)
{
$this->code = $code;
}
public function city($cityname)
{
if (!$this->cities[$cityname])
{
$this->cities[$cityname] = new CITY($cityname);
}
return $this->cities[$cityname];
}
}
class WORLD
{
private $countries = array();
public function country($code = 'us')
{
if (!$this->countries[$code])
{
$this->countries[$code] = new COUNTRY($code);
}
return $this->countries[$code];
}
}
$world = new WORLD;
echo $world->country('us')->city('seattle')->population('employed');
echo $world->country('us')->city('new york')->population();
?>
This example uses Countries and Cities wrapped around a World object. You can use any schema you want, however. Think: Domain->Subdomain->Node or KINGDOM->PHYLUM->CLASS->ORDER->FAMILY->GENUS->SPECIES
What is happening here is that a private array is storing the class objects. Only the class objects that are needed are loaded, when they are needed. You can nest this many many times as needed.
You see that the array is never exposed. Each function reference will check to see if a new class object is needed and create it if necessary.
This is literally as simple as it looks. Hope it helps someone out.
Obeliks
10-Jun-2005 02:56
You can call parent::__construct(), even if the class yours inherits from uses the old constructor style "Classname()".
Example:
<?php
class A {
function A() {
echo 'Constructor of A<br/>';
}
}
class B extends A {
function __construct() {
parent::__construct();
echo 'Constructor of B<br/>';
}
}
class C extends A {
function __construct() {
parent::A();
echo 'Constructor of C<br/>';
}
}
$b = new B();
echo '<br/>';
$c = new C();
?>
So you see you can also call parent::Classname() if your superclass uses this format. Keep in mind that it doesn't work the other way round though:
<?php
class A {
function __construct() {
echo 'Constructor of A<br/>';
}
}
class B extends A {
function __construct() {
parent::A();
echo 'Constructor of B<br/>';
}
}
$b = new B();
?>
So it's always the best choice to use the __construct style! (at least if you're running PHP5)
bartlewis at gmail dot com
08-Jun-2005 06:29
In regards to gaehngaehn at hotmail dot com...
More simply, to create an object from a class using a variable, just try the following.
$a = new $class();
Works in php4 and php5.
Séb.
27-May-2005 09:50
We can't create easily anonymous objects like in JavaScript.
JS example :
var o = {
aProperty : "value",
anotherProperty : [ "element 1", "element 2" ] } ;
alert(o.anotherProperty[1]) ; // "element 2"
So I have created a class Object :
class Object {
function __construct( ) {
$n = func_num_args( ) ;
for ( $i = 0 ; $i < $n ; $i += 2 ) {
$this->{func_get_arg($i)} = func_get_arg($i + 1) ;
}
}
}
$o = new Object(
'aProperty', 'value',
'anotherProperty', array('element 1', 'element 2')) ;
echo $o->anotherProperty[1] ; // "element 2"
You must feel free to make it better :)
AJG
16-May-2005 08:13
[Note from et@php.net:
Your way to create a new, empty, generic object is:
<?php $obj = new stdClass(); ?>
]
I never found a way to create a new, empty, generic object so I coded something up myself. In addition, let me show you some imperfect examples.
<?
$obj1 = (object) '';
$obj2 = (object) 0;
$obj3 = (object) 1;
$obj4 = (object) true;
$obj5 = (object) false;
$obj6 = (object);
$obj7 = (object) NULL;
?>
The first five ones will work, but will create a 'scalar' field in the object. This is undesirable at best, and can be a real nuisance when doing object automation. For that reason, I now use the following function, which produces a 'clean' object. It's also better semantically, in my opinion.
<?
function object() { return ((object) NULL); }
$object = object();
$array = array();
?>
Please post if you see any problems arising due to this method; thanks. Otherwise enjoy.
--AJG.
28-Mar-2005 03:21
There are 3 simple, and utterly annoying problems with your classes (not because of how you want them to work, but because how the Zend II engine handles them):
1) You cannot type hint string or int/integer in a method signature. This is incredibly annoying that the Zend II engine doesn't support it, but there are workarounds.
2) Supplying null in a method signature for a default value means that you will not accept any null value for that parameter. (therefore the method doesn't need to check if the parameters are null anyway).
3) Sadly, overriding methods is only possible with the Zend II engine via Inheritance or Polymorphism, ( and __construct() can only be defined within a class). If you want to override a method in the same class, my suggestion is to provide the method signature with a $flag = null variable, which you call a SWITCH on to pick what the data should do.
==============================================
Other than the afformentioned, the Zend II engine works very similarly to Java, which has made PHP much more versatile and robust in version 5. Thank you again Zend!
spam at afoyi dot com
20-Mar-2005 05:18
You can call a function defined in an inherited class from the parent class. This works in both PHP 4.3.6 and 5.0.0:
<?php
class p {
function p() {
print "Parent's constructor\n";
}
function p_test() {
print "p_test()\n";
$this->c_test();
}
}
class c extends p {
function c() {
print "Child's constructor\n";
parent::p();
}
function c_test() {
print "c_test()\n";
}
}
$obj = new c;
$obj->p_test();
?>
Outputs:
Child's constructor
Parent's constructor
p_test()
c_test()
farzan at ifarzan dot com
05-Oct-2004 04:04
PHP 5 is very very flexible in accessing member variables and member functions. These access methods maybe look unusual and unnecessary at first glance; but they are very useful sometimes; specially when you work with SimpleXML classes and objects. I have posted a similar comment in SimpleXML function reference section, but this one is more comprehensive.
I use the following class as reference for all examples:
<?
class Foo {
public $aMemberVar = 'aMemberVar Member Variable';
public $aFuncName = 'aMemberFunc';
function aMemberFunc() {
print 'Inside `aMemberFunc()`';
}
}
$foo = new Foo;
?>
You can access member variables in an object using another variable as name:
<?
$element = 'aMemberVar';
print $foo->$element; ?>
or use functions:
<?
function getVarName()
{ return 'aMemberVar'; }
print $foo->{getVarName()}; ?>
Important Note: You must surround function name with { and } or PHP would think you are calling a member function of object "foo".
you can use a constant or literal as well:
<?
define(MY_CONSTANT, 'aMemberVar');
print $foo->{MY_CONSTANT}; print $foo->{'aMemberVar'}; ?>
You can use members of other objects as well:
<?
print $foo->{$otherObj->var};
print $foo->{$otherObj->func()};
?>
You can use mathods above to access member functions as well:
<?
print $foo->{'aMemberFunc'}(); print $foo->{$foo->aFuncName}(); ?>
| |
|
| Chiste de... Parecidos razonables | | La luna y el sol | Dos borrachos discutían lo que veían en el cielo: para uno era el sol, y el otro decía que era la luna, no el sol, no la luna.
Y así largo rato hasta que ven venir a otro borracho, y deciden despejar su duda preguntándole a él.
- Disculpe señor, mi amigo y yo tenemos una discusión, y usted nos puede sacar de la duda. Él dice que el que está alla arriba es el sol y yo digo que no, que es la luna. Éste mira el cielo y responde:
- ...vaya...bueno..., ¿saben?...es que yo no soy de este barrio... | | Chistes en tu mail | | ©ContenidosGratis |
| Inicio | Acción | Estrategia | Palabras | Puzzles | Solitarios | Foro Trucos |  | Cake Mania Jugadores: 6835 Categoría del juego: Acción Objetivo del juego: Ayuda a Jill a recuperar la pastelería de su abuela llevando su propia pastelería; consigue clientes y gana dinero. |
|  | Rainbow Web Jugadores: 2199 Categoría del juego: Puzzles Objetivo del juego: Rompe un pegajoso hechizo y salva un reino de fantasía en Rainbow Web. Tendrás toneladas de diversión mientras juegas a este mágico desafío para la mente. |
|  | Mahjongg Fortuna Jugadores: 12462 Categoría del juego: Solitarios Objetivo del juego: Velocidad y habilidad mental son las armas más importantes en esta versión de un antiguo juego asiático. Despeja el tablero lo antes posible haciendo clic en las fichas iguales y gánate la fama eterna de la puntuación más alta. |
|  | Chainz 2 Jugadores: 6955 Categoría del juego: Puzzles Objetivo del juego: Entra en el mundo de las combinaciones con Chainz 2: Relinked, emocionante secuela del exitazo del año pasado, Chainz. Gira eslabones y crea combinaciones de 3 ó más. |
|  | Delicious Jugadores: 4405 Categoría del juego: Acción Objetivo del juego: ¿Eres un as de la multitarea? ¿Quieres que tus clientes estén contentos? ¡Pues Delicious es tu juego! Sacia el apetito de los clientes y tenlos contentos; ¡no te arriesgues! |
|  | Bookworm Jugadores: 4568 Categoría del juego: Palabras Objetivo del juego: Junta las letras para formar palabras. ¡Las palabras más largas valen más puntos! |
|  | Zuma Jugadores: 4976 Categoría del juego: Acción Objetivo del juego: Controla el ídolo de la rana de piedra de los antiguos Zuma en este intrigante enigma de acción. ¡Dispara bolas para formar conjuntos de tres, pero si dejas que lleguen a la calavera dorada morirás! |
|  | Jewel of Atlantis Jugadores: 3798 Categoría del juego: Puzzles Objetivo del juego: Descubre la ciudad hundida de la Atlántida y busca valiosos tesoros. Viaja más allá de las profundidades del mar y vive trepidantes aventuras en Jewel of Atlantis. |
|  | Jewel Quest Jugadores: 3727 Categoría del juego: Puzzles Objetivo del juego: Convierte la arena de la antigua selva en oro tan rápido como puedas juntando grupos de 3 elementos. ¡Los grupos más grandes valen más puntos! |
|  | Bejeweled 2 Jugadores: 3659 Categoría del juego: Puzzles Objetivo del juego: Con cuatro modos de juego únicos y fascinantes, nuevas piezas de juego explosivas e imponentes fondos planetarios, Bejeweled 2 es mucho más adictivo que nunca. |
|
|