php-general Digest 13 Jul 2012 08:05:07 -0000 Issue 7886

Topics (messages 318447 through 318470):

Re: Reverse DNS testing
        318447 by: David OBrien
        318449 by: Al
        318450 by: Al

Entry point of an MVC framework
        318448 by: Simon Dániel
        318451 by: Robert Williams
        318452 by: Daevid Vincent
        318453 by: Ashley Sheridan
        318454 by: Robert Williams
        318455 by: Simon Griffiths
        318456 by: Paul M Foster
        318457 by: Timmy Sjöstedt
        318458 by: admin
        318459 by: Timmy Sjöstedt
        318461 by: Paul M Foster
        318462 by: Paul M Foster
        318463 by: Matijn Woudt

Re: vCard Image
        318460 by: Matijn Woudt

Bazar behavior w/ private member variables
        318464 by: Nathan Nobbe
        318465 by: Tommy Pham
        318466 by: Nathan Nobbe
        318467 by: Nathan Nobbe
        318470 by: Sebastian Krebs

Re: extend or encapsulate?
        318468 by: tamouse mailing lists
        318469 by: marco.behnke.biz

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Jul 12, 2012, at 2:17 PM, Al wrote:

> I want to do a rDNS check on a admin entered host name to insure in-coming 
> mail servers don't reject mail, sent by my app, because the rDNS doesn't 
> exist or doesn't match.
> 
> Here is the fundamental code:
> 
> $host = $_SERVER['SERVER_NAME']; //site name shared or not
> $ip = gethostbyname($host);
> 
> $hostName = gethostbyaddr($ip); //May be different on a shared host
> $ip2 = gethostbyname($hostName);
> 
> The $ip works fine.
> 
> However, one of the shared hosts I'm working with returns this instead of the 
> original $host
> 
> gethostbyaddr($ip)=> 93.247.128.148-static.foo.com [foo is subs for actual]
> 
> gethostbyname($hostName)=> 93.247.128.148-static.foo.com  It appears
> gethostbyname() is just returning $hostName because it is not legit.
> Using just the foo.com in gethostbyname() returns the host's server IP.
> 
> Thus, the typical rDNS check fails for this site. Several online checks also 
> report rDNS fails.
> 
> Any suggestions how I can handle this?
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

It appears the RDNS for that ip has not been mapped to the server name

do you have control of the DNS servers?

can you check the dns config?

--- End Message ---
--- Begin Message ---


On 7/12/2012 3:09 PM, Jim Lucas wrote:
On 07/12/2012 11:17 AM, Al wrote:
I want to do a rDNS check on a admin entered host name to insure
in-coming mail servers don't reject mail, sent by my app, because the
rDNS doesn't exist or doesn't match.

Here is the fundamental code:

$host = $_SERVER['SERVER_NAME']; //site name shared or not
$ip = gethostbyname($host);


$hostName = gethostbyaddr($ip); //May be different on a shared host
$ip2 = gethostbyname($hostName);

Throw in a filter_var() check with the FILTER_VALIDATE_IP flag?

if ( filter_var($hostName, FILTER_VALIDATE_IP) === TRUE ) {
     # This is an IP
     # do something
}

Or do a conditional check

if ( $hostName === $ip2 ) {
     # no change...
     # handle no resolution issue.
}


The $ip works fine.

However, one of the shared hosts I'm working with returns this instead
of the original $host

gethostbyaddr($ip)=> 93.247.128.148-static.foo.com [foo is subs for actual]

gethostbyname($hostName)=> 93.247.128.148-static.foo.com It appears
gethostbyname() is just returning $hostName because it is not legit.
Using just the foo.com in gethostbyname() returns the host's server IP.

Thus, the typical rDNS check fails for this site. Several online checks
also report rDNS fails.

Any suggestions how I can handle this?





I have some additional tests already. Left them out of this dialog to just focus on the essential problem. I check the syntax and stuff before it gets to this code
--- End Message ---
--- Begin Message ---


On 7/12/2012 3:58 PM, David OBrien wrote:

On Jul 12, 2012, at 2:17 PM, Al wrote:

I want to do a rDNS check on a admin entered host name to insure in-coming mail 
servers don't reject mail, sent by my app, because the rDNS doesn't exist or 
doesn't match.

Here is the fundamental code:

$host = $_SERVER['SERVER_NAME']; //site name shared or not
$ip = gethostbyname($host);

$hostName = gethostbyaddr($ip); //May be different on a shared host
$ip2 = gethostbyname($hostName);

The $ip works fine.

However, one of the shared hosts I'm working with returns this instead of the 
original $host

gethostbyaddr($ip)=> 93.247.128.148-static.foo.com [foo is subs for actual]

gethostbyname($hostName)=> 93.247.128.148-static.foo.com  It appears
gethostbyname() is just returning $hostName because it is not legit.
Using just the foo.com in gethostbyname() returns the host's server IP.

Thus, the typical rDNS check fails for this site. Several online checks also 
report rDNS fails.

Any suggestions how I can handle this?


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


It appears the RDNS for that ip has not been mapped to the server name

do you have control of the DNS servers?

can you check the dns config?


Unfortunately, the website is on a typical shared, low cost host. So, I can't get to the DNS record and the outfit's tech support won't help. So, I'm trying to do a reasonable work around in case I run into this issue again on another shared host.
--- End Message ---
--- Begin Message ---
Hi,

I have started to develop a simple MVC framework.

I have a base controller class which is abstract and all of the
controllers are inherited from that. Every controller contains actions
represented by methods. (E. g. there is a controller for managing
product items in a webshop, and there are seperate actions for create,
modify, remove, etc.) There is also a default action (usually an index
page), which is used when nothing is requested.

But what is the best way to invoke an action? I can't do it with the
baseController constructor, becouse parent class can't see inherited
classes. And I can't do it with the constructor of the inherited
class, becouse this way I would overwrite the parent constructor. And
as far as I know, it is not a good practice to call a method outside
of the class, becouse the concept of operation of the class should be
hidden from the other parts of the application.

--- End Message ---
--- Begin Message ---
On 7/12/12 13:21, "Simon Dániel" <simondan...@gmail.com> wrote:


>And I can't do it with the constructor of the inherited
>class, becouse this way I would overwrite the parent constructor.

Just call to the parent constructor from the child:

public function __construct() {
   parent::__construct();
   //do whatever
}

You can call it at any point, so you could do some setup stuff and then
call it, or call it and then do more setup stuff. Basically, you get to
augment the functionality of the parent constructor.

>And as far as I know, it is not a good practice to call a method outside
>of the class, becouse the concept of operation of the class should be
>hidden from the other parts of the application.

Calling methods of other classes is quite normal, and in fact, necessary
in most cases. The trick is that each class specifically decides which
methods it wants to allow outside code to call, which it wants only
children classes to call, and which should only be called internally.
Classes do this by declaring methods as public, protected, or private,
respectively.

<http://us.php.net/manual/en/language.oop5.visibility.php>

What you do want to aim for is a stable set of public methods, which means
that the names of existing methods and the parameters they take don¹t
change when you update the class. If you maintain this stability, then you
can make all the updates you want but not have to update any other code
using the classes. If you change the public methods (that is, change the
class API), then you'll break other code that's using those methods.

Another consideration, and one which you're also touching on, is that of
handling dependencies. If your class requires some other class to
function, you ideally should hand it an instance of the other class,
versus directly instantiating it. You can do this in a variety of ways,
but the most common are passing the object as a parameter to the class
constructor and passing it via an injection method. What you gain by doing
this is that if you want to change the behavior of the class (e.g., by
passing it an object that sends a message by SMS instead of the one it
previously used that sent messages by e-mail), it's a simple matter of
passing a different type of object that has the same public API. This also
makes testing quite a bit easier, since you can pass mock objects that
just pretend to do the functionality of the real ones, thus allowing you
to test the main class without worrying about whether all the secondary
classes upon which it relies will break anything. When you're ready to
learn more about this, do a Google search for "php (inversion of control)".

-Bob

--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/







Notice: This communication, including attachments, may contain information that 
is confidential. It constitutes non-public information intended to be conveyed 
only to the designated recipient(s). If the reader or recipient of this 
communication is not the intended recipient, an employee or agent of the 
intended recipient who is responsible for delivering it to the intended 
recipient, or if you believe that you have received this communication in 
error, please notify the sender immediately by return e-mail and promptly 
delete this e-mail, including attachments without reading or saving them in any 
manner. The unauthorized use, dissemination, distribution, or reproduction of 
this e-mail, including attachments, is prohibited and may be unlawful. If you 
have received this email in error, please notify us immediately by e-mail or 
telephone and delete the e-mail and the attachments (if any).

--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Simon Dániel [mailto:simondan...@gmail.com]
> Sent: Thursday, July 12, 2012 1:21 PM
> Subject: [PHP] Entry point of an MVC framework
> 
> I have started to develop a simple MVC framework.

Yeah! Just what PHP needs, another MVC framework....

NOT.

Why are you re-inventing the wheel?

Personally I *hate* frameworks with a passion, but if you're going to use
one, then why not just build with one that is already out there and well
supported. http://www.phpframeworks.com/ to start with.


--- End Message ---
--- Begin Message ---
On Thu, 2012-07-12 at 14:44 -0700, Daevid Vincent wrote:

> > -----Original Message-----
> > From: Simon Dániel [mailto:simondan...@gmail.com]
> > Sent: Thursday, July 12, 2012 1:21 PM
> > Subject: [PHP] Entry point of an MVC framework
> > 
> > I have started to develop a simple MVC framework.
> 
> Yeah! Just what PHP needs, another MVC framework....
> 
> NOT.
> 
> Why are you re-inventing the wheel?
> 
> Personally I *hate* frameworks with a passion, but if you're going to use
> one, then why not just build with one that is already out there and well
> supported. http://www.phpframeworks.com/ to start with.
> 
> 


A framework is a little more complex than a wheel, much more like the
whole car, and as any real car enthusiast will tell you, it's a lot of
fun building a car from parts :p
-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
On 7/12/12 14:44, "Daevid Vincent" <dae...@daevid.com> wrote:


>Personally I *hate* frameworks with a passion, but if you're going to use
>one, then why not just build with one that is already out there and well
>supported. http://www.phpframeworks.com/ to start with.

I wouldn't suggest most people try to build one to actually use, but that
said, building one for fun is an excellent way to hone your skills,
especially if you're one of those people who just can't seem to come up
with an idea for something better to build that would not only hone your
skills but also be useful to the world :-).


-Bob

--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/







Notice: This communication, including attachments, may contain information that 
is confidential. It constitutes non-public information intended to be conveyed 
only to the designated recipient(s). If the reader or recipient of this 
communication is not the intended recipient, an employee or agent of the 
intended recipient who is responsible for delivering it to the intended 
recipient, or if you believe that you have received this communication in 
error, please notify the sender immediately by return e-mail and promptly 
delete this e-mail, including attachments without reading or saving them in any 
manner. The unauthorized use, dissemination, distribution, or reproduction of 
this e-mail, including attachments, is prohibited and may be unlawful. If you 
have received this email in error, please notify us immediately by e-mail or 
telephone and delete the e-mail and the attachments (if any).

--- End Message ---
--- Begin Message ---
I totally disagree with this!!!!!  !!!! (more exclamation marks) !!!!! (see)
!!! (and some more) !!!!!

Learning how to put design patterns into practice in your chosen language is
a great skill to have as a programmer.  I personally use various frameworks,
however the existence of them does not stop me wanting to build my own,
learn my own way and face the problems of other framework designers and
attempt to understand the problem and how it was solved.  

To me MVC is one of those and cracking it on your own is a great way to
continue learning the language and all it has to offer.

-----Original Message-----
From: Daevid Vincent [mailto:dae...@daevid.com] 
Sent: 12 July 2012 22:44
To: php-gene...@lists.php.net
Subject: RE: [PHP] Entry point of an MVC framework

> -----Original Message-----
> From: Simon Dániel [mailto:simondan...@gmail.com]
> Sent: Thursday, July 12, 2012 1:21 PM
> Subject: [PHP] Entry point of an MVC framework
> 
> I have started to develop a simple MVC framework.

Yeah! Just what PHP needs, another MVC framework....

NOT.

Why are you re-inventing the wheel?

Personally I *hate* frameworks with a passion, but if you're going to use
one, then why not just build with one that is already out there and well
supported. http://www.phpframeworks.com/ to start with.


--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php



--- End Message ---
--- Begin Message ---
On Thu, Jul 12, 2012 at 10:21:21PM +0200, Simon Dániel wrote:

> Hi,
> 
> I have started to develop a simple MVC framework.
> 
> I have a base controller class which is abstract and all of the
> controllers are inherited from that. Every controller contains actions
> represented by methods. (E. g. there is a controller for managing
> product items in a webshop, and there are seperate actions for create,
> modify, remove, etc.) There is also a default action (usually an index
> page), which is used when nothing is requested.
> 
> But what is the best way to invoke an action? I can't do it with the
> baseController constructor, becouse parent class can't see inherited
> classes. And I can't do it with the constructor of the inherited
> class, becouse this way I would overwrite the parent constructor. And
> as far as I know, it is not a good practice to call a method outside
> of the class, becouse the concept of operation of the class should be
> hidden from the other parts of the application.

While I have a progressively growing dislike of frameworks, I'll give
you the following advice from what I've observed in other frameworks:

Normally, you build a controller class elsewhere which inherits from the
base controller class. Let's say it's a controller for blog entries.
Now, you populate the (child) controller with the methods you'll need
for each action. Like a method to delete blog entries. A method for
adding blog entries. Etc.

At that point, you have to decide if you're going to use server
redirection or standard URLs to get to your controller. That is:

http://mysite.com?controller=blog&action=add

or

http://mysite.com/blog/add/

The latter method is preferred by those who believe there's such a
mythical beast as "pretty URLs" preferred by search engines (there
isn't). It requires the use of .htaccess files and Apache's mod_rewrite,
which you may not have in a shared hosting environment.

Hopefully that helps.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

--- End Message ---
--- Begin Message ---


On 07/12/2012 11:44 PM, Daevid Vincent wrote:
-----Original Message-----
From: Simon Dániel [mailto:simondan...@gmail.com]
Sent: Thursday, July 12, 2012 1:21 PM
Subject: [PHP] Entry point of an MVC framework

I have started to develop a simple MVC framework.

Yeah! Just what PHP needs, another MVC framework....

NOT.

Why are you re-inventing the wheel?

Personally I *hate* frameworks with a passion, but if you're going to use
one, then why not just build with one that is already out there and well
supported. http://www.phpframeworks.com/ to start with.



I would say this is a perfectly sane idea if one wants to learn how things actually work.

Most frameworks, however, are very bloated because they have to support and implement many things, as they are general purpose frameworks used by everybody and their dogs.

If you only want what you need, and you know how to do it, rolling your own system is no problem.
--- End Message ---
--- Begin Message ---
-----Original Message-----
From: Timmy Sjöstedt [mailto:m...@iostream.se] 
Sent: Thursday, July 12, 2012 6:01 PM
To: php-gene...@lists.php.net
Subject: Re: [PHP] Entry point of an MVC framework



On 07/12/2012 11:44 PM, Daevid Vincent wrote:
>> -----Original Message-----
>> From: Simon Dániel [mailto:simondan...@gmail.com]
>> Sent: Thursday, July 12, 2012 1:21 PM
>> Subject: [PHP] Entry point of an MVC framework
>>
>> I have started to develop a simple MVC framework.
>
> Yeah! Just what PHP needs, another MVC framework....
>
> NOT.
>
> Why are you re-inventing the wheel?
>
> Personally I *hate* frameworks with a passion, but if you're going to 
> use one, then why not just build with one that is already out there 
> and well supported. http://www.phpframeworks.com/ to start with.
>
>

I would say this is a perfectly sane idea if one wants to learn how things
actually work.

Most frameworks, however, are very bloated because they have to support and
implement many things, as they are general purpose frameworks used by
everybody and their dogs.

If you only want what you need, and you know how to do it, rolling your own
system is no problem.

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php
----------------------------------------------------------------------------
-------------------------

Timmy,
        Thank GOD someone said it. 99.999% of every MVC framework you can
download is crammed to the hilt with BLOAT. I also agree that
knowing/understanding a MVC framework is vital and sometimes the only way to
learn is to recreate the madness you see in front of you.
I like many people have created my OWN MVC framework customized to my
madness and NO BLOAT!!!!!!

I do suggest if you're starting out in MVC frameworks. You have a better
understand of .htaccess and or web.conf for better control and flexibility.



--- End Message ---
--- Begin Message ---
On 07/12/2012 10:21 PM, Simon Dániel wrote:
Hi,

I have started to develop a simple MVC framework.

I have a base controller class which is abstract and all of the
controllers are inherited from that. Every controller contains actions
represented by methods. (E. g. there is a controller for managing
product items in a webshop, and there are seperate actions for create,
modify, remove, etc.) There is also a default action (usually an index
page), which is used when nothing is requested.

But what is the best way to invoke an action? I can't do it with the
baseController constructor, becouse parent class can't see inherited
classes. And I can't do it with the constructor of the inherited
class, becouse this way I would overwrite the parent constructor. And
as far as I know, it is not a good practice to call a method outside
of the class, becouse the concept of operation of the class should be
hidden from the other parts of the application.


By reading your description it sounds like you are using a "C" pattern rather than the "MVC" pattern.

For example you are saying that "there is a controller for managing
product items in a webshop". This should be the job of a Model, not a Controller.

MVC is generally the following:

Model is responsible for data.
View is responsible for presentation of data.
Controller is responsible for connecting Model and View.

In web applications there is also usually some kind of URL Router in front of the Controller.

For example when the framework receives a request to "/items/get_all", the get_all method in ItemsController is executed.

get_all will proceed by loading an ItemModel and executing getAll on it, which returns an array of items.

The controller now loads a new View and passes the array of items to it, which is then rendered as HTML and send to the browser, or whatever the View wants to do.

(If the Model didn't return what we expected, the Controller would load another View that simply contains "I'm a teapot!" or "No items available in this category!")

This is great, because now we can have multiple Controllers accessing the same data without having to inherit or duplicate code (DRY).

The Controllers also doesn't care if the data is stored in MySQL, Postgres, in RAM or on butterflies or anything else, as long as data is returned in an expected format.

The View can of course be changed to another to easily change the appearance (representation) of a resource.

I'd suggest reading some more about MVC before going any further, and other design patterns wouldn't harm either :-)


--- End Message ---
--- Begin Message ---
On Thu, Jul 12, 2012 at 02:44:12PM -0700, Daevid Vincent wrote:

> > -----Original Message-----
> > From: Simon Dániel [mailto:simondan...@gmail.com]
> > Sent: Thursday, July 12, 2012 1:21 PM
> > Subject: [PHP] Entry point of an MVC framework
> > 
> > I have started to develop a simple MVC framework.
> 
> Yeah! Just what PHP needs, another MVC framework....
> 
> NOT.

And here's why Daevid is right. I've been having to work with Symfony, a
well-known framework. Nothing against Symfony, per se. It's much like
other frameworks. However, here's the kicker-- if you make an unknowing
mistake with Symfony (feed it the wrong data at the wrong time), you get
the most beautiful 23-file deep stack trace. Trying to track down why
this parameter (which I didn't even give Symfony) is making it barf is
pure torture. I'm having to wade through deep Symfony code to figure it
out. Object after object instantiating other objects, magic methods,
virtual classes, design pattern classes, etc. I didn't write any of this
code, but I've got to wade through 23 files of it to figure out what the
heck went wrong. And, oh by the way, if you don't call ./symfony cc
(which clears its internal cache) at the right time, you're screwed and
you'll never know why.

Don't get me wrong. MVC is a great idea. It's a good way to divvy up
your code. But frameworks which enforce the paradigm have a way of
getting carried away with themselves. I think a lot of the people who
write them try too hard to impress other programmers with the cleverness
of their designs. By the time they're done, there are hundreds or
thousands of files involved. Every page load has to call in a
significant portion of those files, full of deep, obscure classes. Sure
*looks* kewl. But it's about 15 levels deeper than it has to be.
Remember, this is the web, folks. Your "application" has a lifetime of
one page load, unlike the platform-native accounting program, which
you're liable to keep open on your desktop all day.

I prefer to develop shallow classes which deal with specific aspects of
page handling (dates, databases, encryption, users, etc.). Then use
those as part of the "toolkit", assembled (again on a shallow basis) in
a loosely MVC pattern.

If I make a mistake, I should be able to isolate where it is within an
hour (ideally much less). And be able to go to the specific class and
method involved.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

--- End Message ---
--- Begin Message ---
On Fri, Jul 13, 2012 at 12:26:33AM +0200, Timmy Sjöstedt wrote:


[snip]

> 
> The Controllers also doesn't care if the data is stored in MySQL,
> Postgres, in RAM or on butterflies or anything else, as long as data
> is returned in an expected format.

I can attest that attempting to store data on butterflies is
exceptionally difficult. You really want to isolate that in your models.
Fortunately, the herding_cats design pattern works equally well with
butterflies.

(Welcome to Friday, half a day early! ;-)

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

--- End Message ---
--- Begin Message ---
On Fri, Jul 13, 2012 at 12:41 AM, Paul M Foster <pa...@quillandmouse.com> wrote:
> On Fri, Jul 13, 2012 at 12:26:33AM +0200, Timmy Sjöstedt wrote:
>
>
> [snip]
>
>>
>> The Controllers also doesn't care if the data is stored in MySQL,
>> Postgres, in RAM or on butterflies or anything else, as long as data
>> is returned in an expected format.
>
> I can attest that attempting to store data on butterflies is
> exceptionally difficult. You really want to isolate that in your models.
> Fortunately, the herding_cats design pattern works equally well with
> butterflies.
>
> (Welcome to Friday, half a day early! ;-)
>
> Paul
>

Well Paul, It's Friday over here!:)

Thinking about the butterflies, you could use them to store data (not
ON them). Just put a butterfly in either the left or the right box,
and you have a 1 bit storage. Just one problem, you never know how
long it will take the butterfly to fly from the left to the right, or
the other way around when the gate is open..

- Matijn

--- End Message ---
--- Begin Message ---
On Thu, Jul 12, 2012 at 5:51 PM, Floyd Resler <fres...@adex-intl.com> wrote:
> I'm trying to extract the image from a vCard and display it.  I'm not having 
> any luck.  I saw a sample of putting an image into a vCard via PHP and use 
> base64_encode.  So I thought base64_decode would work.  However, still no 
> luck.  Anyone have any ideas?
>
> Thanks!
> Floyd
>

Hi Floyd,

If I'm not mistaken, vCard is a text based file format. You can open
it in any text editor. You'll see some stuff which just makes sense as
plain text. Images can be embedded inside the file, which is then
indeed in base64, but you'll need to find the start and ending of the
base64 block and only decode that. Keep in mind that images can also
be referred to by URL, in which case you can just download them.

I imagine you can easily find the vCard file format on Google..

- Matijn

--- End Message ---
--- Begin Message ---
Hi all,

Strangely PHP seems to let each class have its own layer of private scope
for member variables.  If a subclass defines a member variable of the same
name as one defined in the parent the values are maintained independently
in instances of the  child class.

First off a simple class with a private member variable $_myPrivate, and a
public accessor method which returns its value:

class A
{
    private $_myPrivate = 5;

    public function getMyPrivate()
    {
        return $this->_myPrivate;
    }
}


Second, a subclass, that gets weird right away, first we define a private
member variable that already has been defined in the parent class, and give
it a different initial value.  To illustrate the behavior we have two
accessor methods, setMyPrivate that uses the $this keyword to get the value
of $_myPrivate, which returns the value of the subclasse's version of the
variable, and getParentsMyPrivate, that calls A::getMyPrivate via the
parent keyword and it returns the value of $_myPrivate as defined in the
base class.

class B extends A
{
    private $_myPrivate = 6;

    public function setMyPrivate()
    {
        $this->_myPrivate = 6;
    }

    public function getMyPrivate()
    {
        return $this->_myPrivate;
    }

    public function getParentsMyPrivate()
    {
        return parent::getMyPrivate();
    }
}


Look at a var_dump of an instance of B:

object(B)#2 (2) {
  ["_myPrivate":"B":private]=>
  int(6)
  ["_myPrivate":"A":private]=>
  int(5)
}

clearly storage is allocated for two different values.  Now I'm sure you
all know that if I were to define a private method in A and try to call it
from B a Fatal error is raised, something on the order of

PHP Fatal error:  Call to private method A::tryToCallMeFromB() from context
'B'

so why the special treatment for member variables, is this supposed to be a
feature?

-nathan

--- End Message ---
--- Begin Message ---
On Thu, Jul 12, 2012 at 7:19 PM, Nathan Nobbe <quickshif...@gmail.com> wrote:
> Hi all,
>
> Strangely PHP seems to let each class have its own layer of private scope
> for member variables.  If a subclass defines a member variable of the same
> name as one defined in the parent the values are maintained independently
> in instances of the  child class.
>
> First off a simple class with a private member variable $_myPrivate, and a
> public accessor method which returns its value:
>
> class A
> {
>     private $_myPrivate = 5;
>
>     public function getMyPrivate()
>     {
>         return $this->_myPrivate;
>     }
> }
>
>
> Second, a subclass, that gets weird right away, first we define a private
> member variable that already has been defined in the parent class, and give
> it a different initial value.  To illustrate the behavior we have two
> accessor methods, setMyPrivate that uses the $this keyword to get the value
> of $_myPrivate, which returns the value of the subclasse's version of the
> variable, and getParentsMyPrivate, that calls A::getMyPrivate via the
> parent keyword and it returns the value of $_myPrivate as defined in the
> base class.
>
> class B extends A
> {
>     private $_myPrivate = 6;
>
>     public function setMyPrivate()
>     {
>         $this->_myPrivate = 6;
>     }
>
>     public function getMyPrivate()
>     {
>         return $this->_myPrivate;
>     }
>
>     public function getParentsMyPrivate()
>     {
>         return parent::getMyPrivate();
>     }
> }
>
>
> Look at a var_dump of an instance of B:
>
> object(B)#2 (2) {
>   ["_myPrivate":"B":private]=>
>   int(6)
>   ["_myPrivate":"A":private]=>
>   int(5)
> }
>
> clearly storage is allocated for two different values.  Now I'm sure you
> all know that if I were to define a private method in A and try to call it
> from B a Fatal error is raised, something on the order of
>
> PHP Fatal error:  Call to private method A::tryToCallMeFromB() from context
> 'B'
>
> so why the special treatment for member variables, is this supposed to be a
> feature?
>
> -nathan

That is OOP accross all languages.  If you want the child class to
modify the variable, then set it to protected.  Private is only
accessible within that class.

--- End Message ---
--- Begin Message ---
On Thu, Jul 12, 2012 at 8:38 PM, Tommy Pham <tommy...@gmail.com> wrote:

> On Thu, Jul 12, 2012 at 7:19 PM, Nathan Nobbe <quickshif...@gmail.com>
> wrote:
> > Hi all,
> >
> > Strangely PHP seems to let each class have its own layer of private scope
> > for member variables.  If a subclass defines a member variable of the
> same
> > name as one defined in the parent the values are maintained independently
> > in instances of the  child class.
> >
> > First off a simple class with a private member variable $_myPrivate, and
> a
> > public accessor method which returns its value:
> >
> > class A
> > {
> >     private $_myPrivate = 5;
> >
> >     public function getMyPrivate()
> >     {
> >         return $this->_myPrivate;
> >     }
> > }
> >
> >
> > Second, a subclass, that gets weird right away, first we define a private
> > member variable that already has been defined in the parent class, and
> give
> > it a different initial value.  To illustrate the behavior we have two
> > accessor methods, setMyPrivate that uses the $this keyword to get the
> value
> > of $_myPrivate, which returns the value of the subclasse's version of the
> > variable, and getParentsMyPrivate, that calls A::getMyPrivate via the
> > parent keyword and it returns the value of $_myPrivate as defined in the
> > base class.
> >
> > class B extends A
> > {
> >     private $_myPrivate = 6;
> >
> >     public function setMyPrivate()
> >     {
> >         $this->_myPrivate = 6;
> >     }
> >
> >     public function getMyPrivate()
> >     {
> >         return $this->_myPrivate;
> >     }
> >
> >     public function getParentsMyPrivate()
> >     {
> >         return parent::getMyPrivate();
> >     }
> > }
> >
> >
> > Look at a var_dump of an instance of B:
> >
> > object(B)#2 (2) {
> >   ["_myPrivate":"B":private]=>
> >   int(6)
> >   ["_myPrivate":"A":private]=>
> >   int(5)
> > }
> >
> > clearly storage is allocated for two different values.  Now I'm sure you
> > all know that if I were to define a private method in A and try to call
> it
> > from B a Fatal error is raised, something on the order of
> >
> > PHP Fatal error:  Call to private method A::tryToCallMeFromB() from
> context
> > 'B'
> >
> > so why the special treatment for member variables, is this supposed to
> be a
> > feature?
> >
> > -nathan
>
> That is OOP accross all languages.  If you want the child class to
> modify the variable, then set it to protected.  Private is only
> accessible within that class.
>

I know that sounds like it should make sense but if it's true, it's an
aspect I've never known about, at least maybe I'm just spacing really bad
or something...

Anyway, this chokes in javac:

public class PrivateAccess
{
    private Boolean isAccessible = true;
}

class PrivateAccessChild extends PrivateAccess
{
    public Boolean getAccessible()
    {
        return isAccessible;
    }
}

PrivateAccessChild.java:5: isAccessible has private access in PrivateAccess
        return isAccessible;
               ^

-nathan

--- End Message ---
--- Begin Message ---
On Thu, Jul 12, 2012 at 9:23 PM, Nathan Nobbe <quickshif...@gmail.com>wrote:

> On Thu, Jul 12, 2012 at 8:38 PM, Tommy Pham <tommy...@gmail.com> wrote:
>
>> On Thu, Jul 12, 2012 at 7:19 PM, Nathan Nobbe <quickshif...@gmail.com>
>> wrote:
>> > Hi all,
>> >
>> > Strangely PHP seems to let each class have its own layer of private
>> scope
>> > for member variables.  If a subclass defines a member variable of the
>> same
>> > name as one defined in the parent the values are maintained
>> independently
>> > in instances of the  child class.
>> >
>> > First off a simple class with a private member variable $_myPrivate,
>> and a
>> > public accessor method which returns its value:
>> >
>> > class A
>> > {
>> >     private $_myPrivate = 5;
>> >
>> >     public function getMyPrivate()
>> >     {
>> >         return $this->_myPrivate;
>> >     }
>> > }
>> >
>> >
>> > Second, a subclass, that gets weird right away, first we define a
>> private
>> > member variable that already has been defined in the parent class, and
>> give
>> > it a different initial value.  To illustrate the behavior we have two
>> > accessor methods, setMyPrivate that uses the $this keyword to get the
>> value
>> > of $_myPrivate, which returns the value of the subclasse's version of
>> the
>> > variable, and getParentsMyPrivate, that calls A::getMyPrivate via the
>> > parent keyword and it returns the value of $_myPrivate as defined in the
>> > base class.
>> >
>> > class B extends A
>> > {
>> >     private $_myPrivate = 6;
>> >
>> >     public function setMyPrivate()
>> >     {
>> >         $this->_myPrivate = 6;
>> >     }
>> >
>> >     public function getMyPrivate()
>> >     {
>> >         return $this->_myPrivate;
>> >     }
>> >
>> >     public function getParentsMyPrivate()
>> >     {
>> >         return parent::getMyPrivate();
>> >     }
>> > }
>> >
>> >
>> > Look at a var_dump of an instance of B:
>> >
>> > object(B)#2 (2) {
>> >   ["_myPrivate":"B":private]=>
>> >   int(6)
>> >   ["_myPrivate":"A":private]=>
>> >   int(5)
>> > }
>> >
>> > clearly storage is allocated for two different values.  Now I'm sure you
>> > all know that if I were to define a private method in A and try to call
>> it
>> > from B a Fatal error is raised, something on the order of
>> >
>> > PHP Fatal error:  Call to private method A::tryToCallMeFromB() from
>> context
>> > 'B'
>> >
>> > so why the special treatment for member variables, is this supposed to
>> be a
>> > feature?
>> >
>> > -nathan
>>
>> That is OOP accross all languages.  If you want the child class to
>> modify the variable, then set it to protected.  Private is only
>> accessible within that class.
>>
>
> I know that sounds like it should make sense but if it's true, it's an
> aspect I've never known about, at least maybe I'm just spacing really bad
> or something...
>
> Anyway, this chokes in javac:
>
> public class PrivateAccess
> {
>     private Boolean isAccessible = true;
> }
>
> class PrivateAccessChild extends PrivateAccess
> {
>     public Boolean getAccessible()
>     {
>         return isAccessible;
>     }
> }
>
> PrivateAccessChild.java:5: isAccessible has private access in PrivateAccess
>         return isAccessible;
>                ^
>
> -nathan
>

Ahhh, but if I add the private declaration in the subclass it works.  Where
have I been??

-nathan

--- End Message ---
--- Begin Message ---
Should go to the mailinlist :>

---------- Forwarded message ----------
From: Sebastian Krebs <krebs....@gmail.com>
Date: 2012/7/13
Subject: Re: [PHP] Bazar behavior w/ private member variables
To: Nathan Nobbe <quickshif...@gmail.com>


Hi,

Private properties are only accessable from within an object of the class,
where it's defined. If you define a new private property with the same name
in a subclass, then you just have two properties with the same name, but in
different classes. They are separate from each other ;) It seems, that you
are _really_ looking for protected properties instead.

Regards,
Sebastian


2012/7/13 Nathan Nobbe <quickshif...@gmail.com>

> On Thu, Jul 12, 2012 at 9:23 PM, Nathan Nobbe <quickshif...@gmail.com
> >wrote:
>
> > On Thu, Jul 12, 2012 at 8:38 PM, Tommy Pham <tommy...@gmail.com> wrote:
> >
> >> On Thu, Jul 12, 2012 at 7:19 PM, Nathan Nobbe <quickshif...@gmail.com>
> >> wrote:
> >> > Hi all,
> >> >
> >> > Strangely PHP seems to let each class have its own layer of private
> >> scope
> >> > for member variables.  If a subclass defines a member variable of the
> >> same
> >> > name as one defined in the parent the values are maintained
> >> independently
> >> > in instances of the  child class.
> >> >
> >> > First off a simple class with a private member variable $_myPrivate,
> >> and a
> >> > public accessor method which returns its value:
> >> >
> >> > class A
> >> > {
> >> >     private $_myPrivate = 5;
> >> >
> >> >     public function getMyPrivate()
> >> >     {
> >> >         return $this->_myPrivate;
> >> >     }
> >> > }
> >> >
> >> >
> >> > Second, a subclass, that gets weird right away, first we define a
> >> private
> >> > member variable that already has been defined in the parent class, and
> >> give
> >> > it a different initial value.  To illustrate the behavior we have two
> >> > accessor methods, setMyPrivate that uses the $this keyword to get the
> >> value
> >> > of $_myPrivate, which returns the value of the subclasse's version of
> >> the
> >> > variable, and getParentsMyPrivate, that calls A::getMyPrivate via the
> >> > parent keyword and it returns the value of $_myPrivate as defined in
> the
> >> > base class.
> >> >
> >> > class B extends A
> >> > {
> >> >     private $_myPrivate = 6;
> >> >
> >> >     public function setMyPrivate()
> >> >     {
> >> >         $this->_myPrivate = 6;
> >> >     }
> >> >
> >> >     public function getMyPrivate()
> >> >     {
> >> >         return $this->_myPrivate;
> >> >     }
> >> >
> >> >     public function getParentsMyPrivate()
> >> >     {
> >> >         return parent::getMyPrivate();
> >> >     }
> >> > }
> >> >
> >> >
> >> > Look at a var_dump of an instance of B:
> >> >
> >> > object(B)#2 (2) {
> >> >   ["_myPrivate":"B":private]=>
> >> >   int(6)
> >> >   ["_myPrivate":"A":private]=>
> >> >   int(5)
> >> > }
> >> >
> >> > clearly storage is allocated for two different values.  Now I'm sure
> you
> >> > all know that if I were to define a private method in A and try to
> call
> >> it
> >> > from B a Fatal error is raised, something on the order of
> >> >
> >> > PHP Fatal error:  Call to private method A::tryToCallMeFromB() from
> >> context
> >> > 'B'
> >> >
> >> > so why the special treatment for member variables, is this supposed to
> >> be a
> >> > feature?
> >> >
> >> > -nathan
> >>
> >> That is OOP accross all languages.  If you want the child class to
> >> modify the variable, then set it to protected.  Private is only
> >> accessible within that class.
> >>
> >
> > I know that sounds like it should make sense but if it's true, it's an
> > aspect I've never known about, at least maybe I'm just spacing really bad
> > or something...
> >
> > Anyway, this chokes in javac:
> >
> > public class PrivateAccess
> > {
> >     private Boolean isAccessible = true;
> > }
> >
> > class PrivateAccessChild extends PrivateAccess
> > {
> >     public Boolean getAccessible()
> >     {
> >         return isAccessible;
> >     }
> > }
> >
> > PrivateAccessChild.java:5: isAccessible has private access in
> PrivateAccess
> >         return isAccessible;
> >                ^
> >
> > -nathan
> >
>
> Ahhh, but if I add the private declaration in the subclass it works.  Where
> have I been??
>
> -nathan
>

--- End Message ---
--- Begin Message ---
It's Friday, so...

Yes, it's true, I have just started looking at using PDO instead of
mysqli -- a bit behind the times...

My question at this stage, is do people tend to extend the PDO class
for their own use, or encapsulate it in a class (or do most people use
it mostly in procedural code?)

--- End Message ---
--- Begin Message ---


tamouse mailing lists <tamouse.li...@gmail.com> hat am 13. Juli 2012 um 07:06
geschrieben:

> It's Friday, so...
>
> Yes, it's true, I have just started looking at using PDO instead of
> mysqli -- a bit behind the times...
>
> My question at this stage, is do people tend to extend the PDO class
> for their own use, or encapsulate it in a class (or do most people use
> it mostly in procedural code?)

I stick to use it as components as it is said "Do not use inheritance for code
re-use" :)


>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
Marco Behnke
Dipl. Informatiker (FH), SAE Audio Engineer Diploma
Zend Certified Engineer PHP 5.3

Tel.: 0174 / 9722336
e-Mail: ma...@behnke.biz

Softwaretechnik Behnke
Heinrich-Heine-Str. 7D
21218 Seevetal

http://www.behnke.biz

--- End Message ---

Reply via email to