omerjerk Posted November 30, 2012 Share Posted November 30, 2012 I want to ask that as we can write objects to a file in C++ and JAVA .. so cav we do the same in PHP ?? If yes then please tell me ... Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/ Share on other sites More sharing options...
MDCode Posted November 30, 2012 Share Posted November 30, 2012 Please explain as to what you mean by "objects." Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396353 Share on other sites More sharing options...
omerjerk Posted November 30, 2012 Author Share Posted November 30, 2012 class omer { my data types } $instance= new omer ; after assigning the values to the data typed of instance , I want to write it in a file as a single entity .... Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396375 Share on other sites More sharing options...
ignace Posted November 30, 2012 Share Posted November 30, 2012 (edited) function object2file($dir, $object) { file_put_contents(rtrim($dir, '\/') . DIRECTORY_SEPARATOR . get_class($object) . '.obj', serialize($object)); } Edited November 30, 2012 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396377 Share on other sites More sharing options...
omerjerk Posted November 30, 2012 Author Share Posted November 30, 2012 function object2file($dir, $object) { file_put_contents(rtrim($dir, '\/') . DIRECTORY_SEPARATOR . get_class($object) . '.obj', serialize($object)); } Plz tell me how to actually use this ... Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396408 Share on other sites More sharing options...
omerjerk Posted November 30, 2012 Author Share Posted November 30, 2012 function object2file($dir, $object) { file_put_contents(rtrim($dir, '\/') . DIRECTORY_SEPARATOR . get_class($object) . '.obj', serialize($object)); } OK ... thanx... It worked fine now ... And the second question is that can't it append the data .,... It is writing the whole file again deleting the previous fie .. Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396414 Share on other sites More sharing options...
trq Posted November 30, 2012 Share Posted November 30, 2012 That is not a question. Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396548 Share on other sites More sharing options...
codefossa Posted November 30, 2012 Share Posted November 30, 2012 That is not a question. lol <3 And the second question is that can't it append the data .,... It is writing the whole file again deleting the previous fie .. Try fopen with an "a" parameter. Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396565 Share on other sites More sharing options...
omerjerk Posted December 1, 2012 Author Share Posted December 1, 2012 lol <3 Try fopen with an "a" parameter. With fopen I cannot write the objects ..... :/ Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396660 Share on other sites More sharing options...
ignace Posted December 1, 2012 Share Posted December 1, 2012 (edited) Have you tried anything so far? Like read the manual page file_put_contents. There is an FILE_APPEND constant that you can pass... function object2file($dir, $object, $mode = 0) { file_put_contents(rtrim($dir, '\/') . DIRECTORY_SEPARATOR . get_class($object) . '.obj', serialize($object), $mode); } object2file('/path/to/dir', new stdClass, FILE_APPEND); Edited December 1, 2012 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/271384-how-to-write-an-object-to-a-file-in-php/#findComment-1396662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.