TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   PROBLEM IN writing code (http://www.talkphp.com/absolute-beginners/4593-problem-writing-code.html)

tech 06-23-2009 10:27 AM

PROBLEM IN writing code
 
dear all..
I wanted to write some code which is already in python..but as i m very new in php not able find which function like hasattr i have to use at that place.can anyone tell me how to write this code in php...
python code is..
def replaceWith(self, replaceWith):
#store old parent
oldParent = self.parent
#get own index in parents contents array
myIndex = self.parent.contents.index(self)
# if mine and replaceWith's parent is same
if hasattr(replaceWith, 'parent') and replaceWith.parent == self.parent:
# We're replacing this element with one of its siblings.
index = self.parent.contents.index(replaceWith)
if index and index < myIndex:
# Furthermore, it comes before this element. That
# means that when we extract it, the index of this
# element will change.
myIndex = myIndex - 1

adamdecaf 06-23-2009 04:25 PM

I can try:

python Code:
def replaceWith(self, replaceWith):

#store old parent
     oldParent = self.parent

#get own index in parents contents array
     myIndex = self.parent.contents.index(self)

# if mine and replaceWith's parent is same
     if hasattr(replaceWith, 'parent') and replaceWith.parent ==     self.parent:

# We're replacing this element with one of its siblings.
index = self.parent.contents.index(replaceWith)
     if index and index < myIndex:

# Furthermore, it comes before this element. That
# means that when we extract it, the index of this
# element will change.
          myIndex = myIndex - 1

"hasattr(object, name)
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)"

So we are just looking to see if an object is viable.

Hmm, I'm stuck (I have very little experience with python), but it looks like your trying to replace objects.

If you had more code or documentation on this it would help.

Wildhoney 06-23-2009 04:48 PM

I think he wants that Python code written into PHP. Hence why I soon closed the thread :-) But at least the code now makes more sense since you've written it up.

adamdecaf 06-23-2009 05:04 PM

Quote:

Originally Posted by Wildhoney (Post 25894)
I think he wants that Python code written into PHP. Hence why I soon closed the thread :-) But at least the code now makes more sense since you've written it up.

I know that he wants the python to be rewritten into PHP, but I was asking for more details on/with the python code.


All times are GMT. The time now is 08:44 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0