Tengo tres clases:
- RegistrarUsuario
- DB
- EnviarEmail
Cuando realizo la clase
RegistrarUsuario tengo que heredar los metodos de la clase
DB, lo hago así:
class RegisterUser extends DB{}
Pero cuando necesito también heredar lo metodos de la clase
EnviarEmail, como sería?
Hola
Acabo de ver esto (http://phpsenior.blogspot.com/2006/08/herencia-mltiple-en-php5.html), no se si será correcto o es lo que buscas pues estoy empezando en PHP
Saludos
Si, eso lo ví, pero no me funciona además dice que no es recomendable hacerlo, dice algo de Interfaces, alguien tiene idea?
Has planteado mal el diseño desde el principio, aunque se puede arreglar.
Mira el ejemplo que acabo de hacer:
<?php
class A {
public $var1, $var2;
public function metodoClaseA() { }
}
class B extends A{
public $var3, $var4;
public function metodoClaseB() { }
}
class Heredero extends B {
public $var5;
public function metodoClaseHeredero() { }
}
$obj = new Heredero();
$obj->metodoClaseA(); //Funciona
print_r($obj);
Otra opción sería la siguiente:
<?php
class A {
public $var1, $var2;
public function metodoClaseA() { echo "estas en clase A"; }
}
class B {
public $var3, $var4;
public function metodoClaseB() { }
public function getMetodosClaseA() { return new A(); }
}
class Heredero extends B {
public $var5;
public function metodoClaseHeredero() { }
}
$obj = new Heredero();
$obj->getMetodosClaseA()->metodoClaseA();
print_r($obj);
La posible solución consiste en crear un objeto de la clase que no puedes heredar y usarlo dentro de esa clase.
Por ejemplo, si dentro de RegistrarUsuario tienes que usar los métodos de DB o EnviarEmail sólo tienes que crear un objeto de una de
esas clases, dentro de RegistrarUsuario.
Ok, Gracias.
Provecho para preguntar, si alguien tiene ejemplo real, de como diseñarlo.
Lo único que no entiendo es como que esta mal planteado el diseño.
http://php.net/manual/es/language.oop5.interfaces.php
Las interfaces son algo asi como 'clases abstractas' que no pueden heredar ni crear objetos, solamente definen prototipos de funciones, y luego el codigo de cada funcion es escrito en la clase que implemente la interfaz.
Yo lo hago así:
class main{
var $need;
function __construct(){
$this->need = array('class1','class2');
}
}
...
function load_lib($lib){
$this->$lib = new $lib();
foreach($this->$lib->need as $need){
$this->$lib->$need = new $need();
}
}
Se que no es lo mas adecuado ni lo mas correcto pero yo suelo utilizar funciones que recursivamente agregan las librerias que requiero desde un array dentro del constructor, es parte de mi framework y se cargan inmediatamente cuando las llamo y quedan establecidas en la clase principal del sistema incluyendo las incluidas de forma recursiva, o sea, cada clase cargada a su ves hereda todas las demás clases por defecto y puedes llamarlas desde donde te sea mas cómodo.
Mira, dale un vistazo al archivo principal de mi framework:
<?php if($_SERVER['SCRIPT_FILENAME'] == __file__) exit;
/* FrameworkDrawCoders : 3.3 / Last modification : 20-Oct-2011 Class version : 1.6 */
/* Execute */
$system = new system();
$system->prepare_permalinks();
$system->stripslash_superglobals();
$system->load_plugins('pre');
$system->load_module();
$system->load_theme();
$system->load_plugins('pos');
$system->theme->show_all();
$system->terminate();
/* End execute */
class system{
/* Class vars */
var $sidebars;
var $main_menu;
/* System Vars */
var $last_error;
/* Default libs */
var $conf;
var $str;
var $path;
var $headers;
function __construct(){
include(dirname(__file__).'/configuration.php');
$this->conf = new configuration();
$this->conf->apply();
/* Global functions */
$this->load_global_libs();
$this->headers->ctype_html();
}
function load_global_libs(){
/* Make matrix */
foreach($this->conf->load_default_libs as $lib){
include_once($this->conf->lib_dir .$lib.'.php');
if(!is_object($this->$lib)){
$this->$lib = new $lib();
$this->$lib->conf = $this->conf;
}
}
/* Inject objects */
foreach($this->conf->load_default_libs as $lib){
foreach($this->conf->load_default_libs as $inject){
if((is_object($this->$inject)) and ($inject != $lib)){
$this->$lib->$inject = $this->$inject;
}
}
}
}
function load_ret_lib($lib){
if(!$lib = preg_replace('|[^A-Za-z0-9_]|i', '', $lib))
return false;
if(!include_once($this->conf->lib_dir .$lib.'.php'))
return false;
$retOB = new $lib;
/* Import default libs */
foreach($this->conf->load_default_libs as $lib_def){
if($lib_def != $lib){
if($this->$lib_def)
$retOB->$lib_def = $this->$lib_def;
else{
include_once($this->conf->lib_dir .$lib_def.'.php');
$retOB->$lib_def = new $lib_def;
}
}
}
/* Configurations */
$retOB->conf = $this->conf;
/* Import libs needs */
if(is_array($retOB->libs_need)){
foreach($retOB->libs_need as $import){
if($this->$import)
$retOB->$import = $this->$import;
else{
include_once($this->conf->lib_dir .$import.'.php');
$retOB->$import = new $import;
}
}
}
return $retOB;
}
function load_lib($lib){
if(!$lib = preg_replace('|[^A-Za-z0-9_]|i', '', $lib))
return false;
if(is_object($this->$lib))
return $this->$lib;
if(!include_once($this->conf->lib_dir .$lib.'.php'))
return false;
$this->$lib = new $lib();
/* Import default libs */
foreach($this->conf->load_default_libs as $lib_def){
if((is_object($this->$lib_def)) and ($lib_def != $lib)){
$this->$lib->$lib_def = $this->$lib_def;
}
}
/* Configurations */
$this->$lib->conf = $this->conf;
/* Import libs needs */
if(is_array($this->$lib->libs_need)){
foreach($this->$lib->libs_need as $import){
$this->load_lib($import); /* Make lib */
$this->$lib->$import = $this->$import; /* Import lib */
}
}
return $this->$lib;
}
function load_plugins($prefix){
if($plugins = glob($this->path->plugins('local').$prefix.'*.php')){
foreach($plugins as $plugin){
include_once($plugin);
}
}
}
function load_module(){
ob_start();
if($inc = $this->mod_selected()){
$this->conf->module_used_dir = $this->path->modules('relative') .$inc.'/';
$this->conf->module_selected = $inc;
}else{
$this->conf->last_code_status = 404;
$this->conf->module_used_dir = $this->path->modules('relative') .'status/';
$this->conf->module_selected = 'status';
}
include_once($this->path->actual_mod('local') .'index.php'); /* Main file of module */
$this->theme->buffer_module = ob_get_contents();
ob_end_clean();
}
function load_component($file){
ob_start();
if(file_exists($inc = $this->path->actual_mod('local') .'/__layout/'.$file.'.php')){
include_once($inc); /* Actual mod */
}elseif(file_exists($inc = $this->path->modules('local') .$this->conf->main_mod .'/__layout/'.$file.'.php')){
include_once($inc); /* Main mod */
}
$buff = ob_get_contents();
ob_end_clean();
return $buff;
}
function load_theme(){
if($this->conf->use_theme){
/* Load components */
$this->theme->buffer_footer = $this->load_component('footer');
$this->load_component('main_menu');
$this->load_component('sidebars');
/* Load theme */
$this->conf->theme_dir = $this->conf->themes_dir .$this->conf->id_theme .'/';
if(file_exists($this->path->theme('local').'index.php')){
ob_start();
include($this->path->theme('local').'index.php');
$this->theme->buffer_module = ''; /* less memory */
$this->theme->buffer_theme = ob_get_contents();
ob_end_clean();
}else
$this->theme->buffer_theme = 'Theme not exist.';
}
}
function terminate(){
if(is_object($this->sql)){
if($this->sql->handle)
$this->sql->close();
}
}
function mod_selected(){
if(!isset($_GET[$this->conf->var_get]))
return $this->conf->main_mod;
if(!$inc = preg_replace('|[^A-Za-z0-9_,-]|i', '', $_GET[$this->conf->var_get]))
return false;
if(!file_exists($this->conf->modules_dir .$inc.'/index.php'))
return false;
return $inc;
}
function stripslash_superglobals(){
if(get_magic_quotes_gpc()){
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_SERVER = array_map('stripslashes', $_SERVER);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_REQUEST = array_map('stripslashes', $_REQUEST);
$_ENV = array_map('stripslashes', $_ENV);
}
}
function prepare_permalinks(){
if(isset($_SERVER['REDIRECT_URL'])){ /* Mod rewrite -> to php */
if(dirname($_SERVER['SCRIPT_NAME']) != '/')
$permalink = str_replace(array(dirname($_SERVER['SCRIPT_NAME']), '.html'), '', $_SERVER['REDIRECT_URL']); // /permalink/x/aaaaa.html
else
$permalink = str_replace(array('.html'), '', $_SERVER['REDIRECT_URL']); // /permalink/x/aaaaa.html
if(substr($permalink, 0, 1) == '/')
$permalink = substr($permalink, 1);
$permalink = explode('/', $permalink);
foreach($permalink as $id => $var){
if($id == 0){
$_GET[$this->conf->var_get] = $var;
}elseif($id % 2 == 0){
$_GET[$prevar] = $var;
unset($prevar);
}else{
$prevar = $var;
}
unset($id, $var);
}
unset($permalink);
if($prevar)
unset($prevar);
}
}
}
Después hago esto:
<?php
$this->load_lib('paginator');
$this->paginator->load_sql('select * from {prefix}users');
$this->paginator->makeHtml();
echo 'Con un total de '.$this->paginator->pages->toInt().' paginas.';
?>
Y si te fijas llamé a la clase paginator que se cargó dentro de la clase principal y a su ves paginator requiere por defecto la clase str que me transforma las variables a objetos para pharsearlas como por ejemplo $this->user->name->toHtml() o $this->str->_get('algo')->toHtml() y son solo recursiones simples:
<?php if($_SERVER['SCRIPT_FILENAME'] == __file__) exit;
/*
FrameworkDrawCoders : 3.*
Last modification : 29-4-2011
Class version : 1.3
*/
class user{
/* System Vars */
var $last_error;
var $libs_need;
/* Default libs */
var $conf;
var $str;
var $path;
var $headers;
/* Libs need */
var $sql;
/* Class Vars */
var $is_logged = false;
var $is_loaded = false;
var $user_data = false;
function __construct(){
$this->libs_need = array('sql'); /* Need */
}
function load($arr = false){
if(is_array($arr)){
$find = '';
$count = 0;
foreach($arr as $var => $val){
$val = new str($val);
$find .= $var.' = "'.$val->toSql().'"';
$count++;
if($count != count($arr))
$find .= ' and ';
}
if(!$this->user_data = $this->sql->fast_select('select * from {prefix}users where '.$find.' limit 1', true))
$this->last_error = array('id' => 1, 'text' => 'Value(s) dont match');
else{
foreach($this->user_data as $var => $val){
$this->$var = new str($val);
}
$this->is_loaded = true;
}
}
return $this;
}
function login($arr_condition = false){
if(!$this->is_loaded){
$this->last_error = array('id' => 13, 'text' => 'Is not loaded');
return $this;
}
if(is_array($arr_condition)){
if($arr_condition['password'])
$arr_condition['password'] = $this->cryptPass($arr_condition['password']);
foreach($arr_condition as $var => $val){
if($this->$var->val() != $val){
$this->last_error = array('id' => 14, 'text' => 'Values dont match');
return $this;
}
}
} /* else{ Autologin without data } */
/* Update database */
$this->update(array(
'token' => $this->makeHash(),
'last_login' => time()
));
/* Set the cookie */
setcookie($this->conf->cookiename , serialize(array(
'id_user' => $this->id->toInt(),
'password' => $this->password->val(),
'token' => $this->token->val()
)), false, '/');
$this->is_logged = true;
return $this;
}
function loadCookie(){
if($cookie = unserialize(trim($_COOKIE[$this->conf->cookiename]))){
$this->load(array(
'id' => (int)$cookie['id_user'],
'password' => $cookie['password'],
'token' => $cookie['token']
));
if($this->is_loaded){
$this->is_logged = true;
$this->update(array('last_login' => time()));
}
}else
$this->last_error = array('id' => 3, 'text' => 'No cookie to login');
return $this;
}
function logout(){
if($this->is_loaded and $this->is_logged){
$this->update(array('token' => $this->makeHash())); /* AntiHacking account->cookie */
setcookie($this->conf->cookiename, '', false, '/');
$this->is_logged = false;
foreach($this->user_data as $var => $val){
unset($this->$var);
}
$this->user_data = '';
$this->is_loaded = false;
}else{
$this->last_error = array('id' => 16, 'text' => 'No cookie to login');
}
return $this;
}
function newAccount($arr_data){
/* Set default data */
if($arr_data['password']) $arr_data['password'] = $this->cryptPass($arr_data['password']);
if(!$arr_data['token']) $arr_data['token'] = $this->makeHash();
if(!$arr_data['register_date']) $arr_data['register_date'] = time();
if(!$this->sql->update('users', $arr_data))
$this->last_error = array('id' => 8, 'text' => $this->sql->last_error['text']);
return $this;
}
function update($arr_data){
if(!$this->is_loaded){
$this->last_error = array('id' => 9, 'text' => 'No data loaded for update');
return $this;
}
if($arr_data['password'])
$arr_data['password'] = $this->cryptPass($arr_data['password']);
foreach($arr_data as $var => $val){
$this->user_data[$var] = $val;
$this->$var = new str($val);
}
return $this->sql->update('users', $arr_data, 'id = '.$this->id->toInt());
}
function delete(){
if($this->sql->raw('delete from {prefix}users where id = '.$this->id->toInt())){
$this->is_logged = false;
$this->is_loaded = false;
if(is_array($this->user_data)){
foreach($this->user_data as $var => $val){
unset($this->$var);
}
$this->user_data = false;
}
}else
$this->last_error = array('id' => 11, 'text' => 'Error in delete user. Db error');
}
function makeHash($length = 10){
return substr(sha1($length.rand(11,99).microtime()), 0, (int)$length);
}
function cryptPass($password){
return sha1($password.sha1("\xa1__".$password)."__\x01");
}
function levelPass($password = false){
if(!$password) return 0;
$level = 0;
if(strlen($password) > 1) $level += 1;
if(strlen($password) > 2) $level += 1;
if(strlen($password) > 3) $level += 3;
if(strlen($password) > 4) $level += 5;
if(strlen($password) > 5) $level += 10;
if(strlen($password) > 6) $level += 10;
if(strlen($password) > 7) $level += 10;
if(strlen($password) > 10) $level += 10;
if(strlen($password) > 20) $level += 25;
if(!ctype_alnum(payload)) $level += 25;
return $level;
}
}
Si te fijas en este caso no fue necesario solicitar la clase str en el array porque el sistema ya carga algunas clases por default en el archivo de configuraciones que son str, conf y path.
Si le hago print_r() a la clase se pueden ver las recursiones, si modificas $this->conf es lo mismo que modificar $this->algo->conf
Citar
print_r($this->user); exit;
...
[str] => str Object
*RECURSION*
[path] =>
[headers] => headers Object
(
[last_error] =>
[libs_need] =>
[conf] => configuration Object
(
[script_url] => http://127.0.0.1/x/index.php
...