php-general Digest 22 Sep 2010 15:22:35 -0000 Issue 6954
Topics (messages 308250 through 308257):
ZipArchive, but without files
308250 by: Jonathan Mills
308251 by: Viacheslav Chumushuk
Copying an Object
308252 by: Daniel Kolbo
308253 by: mr bungle
308254 by: chris h
308255 by: Nathan Nobbe
308256 by: Nathan Nobbe
308257 by: Carlos Medina
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
Is it possible to create a ZipArchive to a non-file storage, eg string?
I want to create a small zip of some data, and then load it into a database.
I don't need the intermediate file.
I tried various filters, and I can READ a zip archive with the
"data://" wrapper, but haven't found any way of creating the data
other than in a file yet.
Jonathan
--- End Message ---
--- Begin Message ---
Hello.
As I understand you in a right way you need next function
http://ua.php.net/manual/en/function.gzcompress.php
On Wed, Sep 22, 2010 at 10:16:30AM +0100, Jonathan Mills
<[email protected]> wrote:
> Is it possible to create a ZipArchive to a non-file storage, eg string?
>
> I want to create a small zip of some data, and then load it into a database.
>
> I don't need the intermediate file.
>
> I tried various filters, and I can READ a zip archive with the
> "data://" wrapper, but haven't found any way of creating the data
> other than in a file yet.
>
> Jonathan
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
--
Best regards, Viacheslav Chumushuk
email/jabber: [email protected]
Ukraine, Khmelnitsky
- - - - -
Please, use plain text message format contacting me, and
don't use proprietary formats for attachments (such as DOC, XLS)
use PDF, TXT, ODT, HTML instead. Thanks.
--- End Message ---
--- Begin Message ---
Hello PHPers,
I have:
class A {
...code
}
class B extends A {
...code
}
$a = new A();
$b = new B();
I would like to get all of the properties of $a into $b by value. Class
A extends 3 other classes. I would like a way to not have to manage a
'copy' method in B if A or one of the subclasses of A change.
I was reading about clone, but this doesn't really seem to help me in
this situation.
How can I copy $a into $b?
Thanks,
dK
`
--- End Message ---
--- Begin Message ---
Maybe statics members can help you with this ...
On Wed, Sep 22, 2010 at 07:35, Daniel Kolbo <[email protected]> wrote:
> Hello PHPers,
>
> I have:
>
> class A {
> ...code
> }
>
> class B extends A {
> ...code
> }
>
> $a = new A();
>
> $b = new B();
>
> I would like to get all of the properties of $a into $b by value. Class
> A extends 3 other classes. I would like a way to not have to manage a
> 'copy' method in B if A or one of the subclasses of A change.
>
> I was reading about clone, but this doesn't really seem to help me in
> this situation.
>
> How can I copy $a into $b?
>
> Thanks,
> dK
> `
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Ellis 1100101000111101
--- End Message ---
--- Begin Message ---
You could create a method of class B that takes an object of class A as a
parameter and copies each property line by line (or method of class A that
takes an object of class B...). If you don't want to add a method you could
just do the same thing, but procedurally. The issue with this (aside from
being bad oop) is that you can't copy private properties unless you have all
the required getters and setters. The issue with both of these is that it's
ugly, high maintenance code.
There is the iterator class, extending from which would allow you iterate
through all of your properties in a foreach, but if you don't want to add a
new method then you likely don't want to add a parent class.
I don't care for any of these options, but as far as I know there's no
internal PHP mechanism to to copy all the properties from one object to
another object of a different class - please correct me if I'm wrong. Is it
possible that there's a more elegant solution to your problem that does not
include a mass copy of all an object's properties? (e.g. using statics like
Mr Bungle suggested or perhaps some nifty design pattern?)
Chris H.
On Wed, Sep 22, 2010 at 7:35 AM, Daniel Kolbo <[email protected]> wrote:
> Hello PHPers,
>
> I have:
>
> class A {
> ...code
> }
>
> class B extends A {
> ...code
> }
>
> $a = new A();
>
> $b = new B();
>
> I would like to get all of the properties of $a into $b by value. Class
> A extends 3 other classes. I would like a way to not have to manage a
> 'copy' method in B if A or one of the subclasses of A change.
>
> I was reading about clone, but this doesn't really seem to help me in
> this situation.
>
> How can I copy $a into $b?
>
> Thanks,
> dK
> `
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
On Wed, Sep 22, 2010 at 5:35 AM, Daniel Kolbo <[email protected]> wrote:
> Hello PHPers,
>
> I have:
>
> class A {
> ...code
> }
>
> class B extends A {
> ...code
> }
>
> $a = new A();
>
> $b = new B();
>
> I would like to get all of the properties of $a into $b by value.
first off you have to be careful here since there is no real explicit copy
by value in php since internally the engine uses copy on write. there is a
caveat in __clone however, where you can force new instances of objects to
be created in cloned instance variables rather than a reference to the
original object in member variables of the cloned instance.
> Class
> A extends 3 other classes. I would like a way to not have to manage a
> 'copy' method in B if A or one of the subclasses of A change.
>
well since B extends A directly here, obviously theres no need to worry
about other subclasses of A changing when considering the copy constructor
(__clone in php) of B. anyway if A changes you need only potentially revise
the __clone() implementation of A.
> I was reading about clone, but this doesn't really seem to help me in
> this situation.
>
egh? define __clone() in A to be responsible for any member variables
defined in A then define __clone() in B to be responsible for any additional
member variables defined therein, making sure to invoke parent::__clone()
from B. naturally if you add or remove member variables from A you revise
the __clone() implementation of A, the same is true for B.
this isnt a maintenance nightmare and is also a sound oop practice. the
same idea would be applied to a copy constructor in java or c++.
note also that php internally manages a shallow copy out of the box, meaning
you wont have to implement any code to 'copy' non-object member variables in
cloned objects. you will however have to determine if a reference to the
same objects in instance variables of cloned objects is appropriate or if
each instance of the class you are cloning needs to have their own instance
of member variables referencing objects.
here is a somewhat contrived example which illustrates everything. all you
have to do is run the script and inspect the id of each object stored in
member variables. what youll find is that in each instance of B shallowObj
is referring to the same instance of stdClass, yet anotherObj from B and
someObj from A are each explicitly 'copied' for each instance of B. also
youll notice that A manages cloning of someObj and B manages cloning of
anotherObj, keeping in line with encapsulation. also notice the scalar
variables are 'copied' by the engine w/ no work in userspace.
<?php
class A
{
public $blah = 0;
protected $meh = 1;
private $care = 2;
private $someObj = null;
public function __construct()
{
$this->someObj = new stdClass();
}
public function __clone()
{
// note only if we want to force a new instance of
// stdClass into $this->someObj in cloned instance of A
$this->someObj = clone $this->someObj;
}
}
class B extends A
{
protected $what = 5;
protected $anotherObj = null;
protected $shallowObj = null;
public function __construct()
{
parent::__construct();
$this->anotherObj = new stdClass();
$this->shallowObj = new stdClass();
}
public function __clone()
{
// make sure to invoke parent __clone()
parent::__clone();
// note as in A::__clone() above, forcing new instance
// of $this->anotherObj in cloned instance of B
$this->anotherObj = clone $this->anotherObj;
// note also that we do not clone $this->shallowObj here,
// meaning each instance of B will reference the same instance of
$this->shallowObj
}
}
$b1 = new B();
$b2 = clone $b1;
var_dump(array(
$b1, $b2
));
?>
/// output
array(2) {
[0]=>
object(B)#1 (7) {
["what":protected]=>
int(5)
["anotherObj":protected]=>
object(stdClass)#3 (0) {
}
["shallowObj":protected]=>
object(stdClass)#4 (0) {
}
["blah"]=>
int(0)
["meh":protected]=>
int(1)
["care":"A":private]=>
int(2)
["someObj":"A":private]=>
object(stdClass)#2 (0) {
}
}
[1]=>
object(B)#5 (7) {
["what":protected]=>
int(5)
["anotherObj":protected]=>
object(stdClass)#7 (0) {
}
["shallowObj":protected]=>
object(stdClass)#4 (0) {
}
["blah"]=>
int(0)
["meh":protected]=>
int(1)
["care":"A":private]=>
int(2)
["someObj":"A":private]=>
object(stdClass)#6 (0) {
}
}
}
hope it helps!
-nathan
--- End Message ---
--- Begin Message ---
On Wed, Sep 22, 2010 at 5:35 AM, Daniel Kolbo <[email protected]> wrote:
> Hello PHPers,
>
> I have:
>
> class A {
> ...code
> }
>
> class B extends A {
> ...code
> }
>
> $a = new A();
>
> $b = new B();
>
after looking back over your situation, which looks strange, why not use
composition at this level,
// note untested, may have some syntax errors
class B
{
private $a = null;
public function __construct(A $a)
{
$this->a = $a;
}
public function swapA(A $a)
{
$this->a = $a;
}
// proxy any calls to A which you would like to appear transparent
public function __call($method, array $args)
{
return call_user_func_array(array($this->a, $method), $args);
}
// note requires 5.3
public function __callStatic($method, array $args)
{
return call_user_func_array(array($this->a, $method), $args);
}
}
$a = new A();
$b = new B($a);
// some code ..
$anotherA = new A();
// some more code
$b->swapA($anotherA);
-nathan
--- End Message ---
--- Begin Message ---
Am 22.09.2010 13:35, schrieb Daniel Kolbo:
Hello PHPers,
I have:
class A {
...code
}
class B extends A {
...code
}
$a = new A();
$b = new B();
I would like to get all of the properties of $a into $b by value. Class
A extends 3 other classes. I would like a way to not have to manage a
'copy' method in B if A or one of the subclasses of A change.
I was reading about clone, but this doesn't really seem to help me in
this situation.
How can I copy $a into $b?
Thanks,
dK
`
Hi dk,
by using inheritance only you will bind your code to an specific
implementation of code. Wherever you want to do with your classes, i
think is the better way to use some design patterns to build classes by
the functionality or by structure.
The use of composition allow to manage your classes flexible very well.
But look for Factory or Decorator (maybe Command in this case) to solve
the class inheritance.
Regards
Carlos Medina
--- End Message ---