PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 24th, 2012, 05:40 AM
gino8 gino8 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 gino8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 2 sec
Reputation Power: 0
PHP-OOP - Array from parent class to subclass

Hi!

look this simple example:

class myClass {

var $lang = 'en';
var $translations = array();

public function __construct($lang,$translations = array()) {
$this->lang = $lang;
$this->array_translations = $translations;
}

public function foo() {
echo $this->lang;
echo $this->array_translations['2'];
}
}


class myClass2 extends myClass {
public function __construct() {
}

public function foo2() {
echo $this->lang;
echo $this->array_translations['2'];
}

}


$class = new myClass($lang,$translations);

$class2 = new myClass2();
$class2->foo2();

Why array_translations doesn't read in class myClass 2? Lang yes.. but array elements no? I don't understand

Reply With Quote
  #2  
Old November 24th, 2012, 07:29 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,681 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 2 h 20 m 36 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Because
1. You didn't give myClass2 the list of translations. Or the language for that matter - it's showing you "en" because that's the default value.
2. You set up your own __construct which does not set $lang or $array_translations. If you want it to do that then (a) get rid of your version and let myClass's __construct take over, or (b) make your version call parent::__construct(...) with the right arguments.

Reply With Quote
  #3  
Old November 24th, 2012, 08:17 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,837 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 5 h 25 m 30 sec
Reputation Power: 811
Hi,

this is a misunderstanding of how objects work. The $translations property is an instance property, so every object has its own $translations. Setting the value for a particular object has no effect on the other instances that might exist at this point of time.

Think of a simpler example: If create a Mammal with the name "Peter" (could be a human, a dog, a cat, whatever), does that mean that all Humans (which are a subclass of Mammals) should now be named "Peter"?

Reply With Quote
  #4  
Old November 24th, 2012, 12:04 PM
gino8 gino8 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 gino8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
Because
1. You didn't give myClass2 the list of translations. Or the language for that matter - it's showing you "en" because that's the default value.
2. You set up your own __construct which does not set $lang or $array_translations. If you want it to do that then (a) get rid of your version and let myClass's __construct take over, or (b) make your version call parent::__construct(...) with the right arguments.


Thanks for your answer.

I'have my array included in all page of my site:

$translations = array();
$translations['1'] = 'text1';
$translations['2'] = 'text2';
$translations['3'] = 'text3';
$translations['4'] = 'text4';
$translations['5'] = 'text5';
$translations['6'] = 'ecc';

I would like include this array element in my Class for being used in every subclass.

Is it possible?

Reply With Quote
  #5  
Old November 24th, 2012, 12:44 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,837 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 5 h 25 m 30 sec
Reputation Power: 811
If the array is known from the beginning, use a static property and set the array as the default value:

PHP Code:
<?php

class {

    static 
$x = array(123);

}

class 
extends {

    function 
showX() {
        
var_dumpself::$x );
    }

}

$b = new B();
$b->showX();


This way all subclasses will have a static property $x (or $translations in your case) set to a certain array. Note that the subclasses can still overwrite it. If you also want to prevent that, you have to access the property through getter/setter methods.

By the way, why do you use number strings as array indices? And why do you set the elements one after another instead of using a much shorter array literal?
PHP Code:
 $x = array(
    
'a'
    
'b'
    
'c'
); 

Last edited by Jacques1 : November 24th, 2012 at 12:47 PM.

Reply With Quote
  #6  
Old November 24th, 2012, 01:02 PM
gino8 gino8 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 gino8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by Jacques1
If the array is known from the beginning, use a static property and set the array as the default value:

PHP Code:
<?php

class {

    static 
$x = array(123);

}

class 
extends {

    function 
showX() {
        
var_dumpself::$x );
    }

}

$b = new B();
$b->showX();


This way all subclasses will have a static property $x (or $translations in your case) set to a certain array. Note that the subclasses can still overwrite it. If you also want to prevent that, you have to access the property through getter/setter methods.

By the way, why do you use number strings as array indices? And why do you set the elements one after another instead of using a much shorter array literal?
PHP Code:
 $x = array(
    
'a'
    
'b'
    
'c'
); 


Ok, but my array is external... is included in all pages, and contain more elements.
The problem is setting external array into static element.

Reply With Quote
  #7  
Old November 24th, 2012, 04:01 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,837 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 5 h 25 m 30 sec
Reputation Power: 811
Then use a setter method.

PHP Code:
<?php

class {

    private static 
$x;
    
    static function 
getX() {
        return 
self::$x;
    }
    
    static function 
setX($x) {
        
self::$x $x;
    }

}

class 
extends {

    function 
showX() {
        
var_dumpself::getX() );
    }

}

$translations = array(
    
'foo'
    
'bar'
);

A::setX($translations);
$b = new B();
$b->showX();

Reply With Quote
  #8  
Old November 25th, 2012, 04:06 AM
gino8 gino8 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 gino8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by Jacques1
Then use a setter method.

PHP Code:
<?php

class {

    private static 
$x;
    
    static function 
getX() {
        return 
self::$x;
    }
    
    static function 
setX($x) {
        
self::$x $x;
    }

}

class 
extends {

    function 
showX() {
        
var_dumpself::getX() );
    }

}

$translations = array(
    
'foo'
    
'bar'
);

A::setX($translations);
$b = new B();
$b->showX();



Many thanks Jacques1!

you have been useful

Reply With Quote
  #9  
Old November 25th, 2012, 04:16 AM
gino8 gino8 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 gino8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by gino8
Many thanks Jacques1!

you have been useful


Ps it's strange that you have to assign the static element out of the classroom

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-OOP - Array from parent class to subclass

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap