0

I was hoping someone can help me achieve the same structure in php. Here is what I do in my .NET class.

Public Class objSample

Private _MyPrivateVar As Boolean = False

Public Property MyPublicProperty As Boolean
    Get
        Return _MyPrivateVar
    End Get
    Set(ByVal value As Boolean)
        _MyPrivateVar = value
    End Set
End Property

End Class
joeb
  • 689
  • 7
  • 25

2 Answers2

1

Here it is:

class objSample
{
   private $_MyPrivateVar = false;

   public $_MyPublicProperty;

   public function getMyPrivateVar()
   {
       return $this->_MyPrivateVar;
   }

   public function setMyPrivateVar($val)
   {
       $this->_MyPrivateVar = $val;
   }
}
Yang
  • 8,431
  • 7
  • 32
  • 55
  • you see this is where I've gotten confused. Because both of your are correct but I'm not sure the usage is the same. In vb.net I can use `Dim NewVal as boolean = objSample.myPublicProperty` like this to get an object. And I can also use the same to set it like this `objSample.myPublicProperty = 6`. Now I was reading about PHP's magic functions which is what I see in the posting from Royal Bg. Which seems like its almost there. – joeb Aug 24 '13 at 01:15
  • But now in what you've shown me here I would have to use this to set `objPHP::setMyPrivateVar(6)` which means for every property i would also have to define set and get functions which is killer on code. So I am not sure which direction to go in. – joeb Aug 24 '13 at 01:16
  • I guess let me just ask the question. How would you set a private property from outside of the class in php? Whats the best method? – joeb Aug 24 '13 at 01:17
  • PHP doesn't support native class setters and getters, like C# does, for example. You asked a specific question on how to do `X` thing, I answered it. If you need a dynamic property setter and getter, this answer might help you: http://stackoverflow.com/a/15796245/1208233 – Yang Aug 24 '13 at 01:23
0

I have no idea what's the purpose of this code, having lack of VB knowledge, it seems it has practically non use. However, I tried to make it as I think it has to work. Maybe someone more experienced with Visual Basic could help too. But, have in mind, we are not here to convert code. You should investigate PHP for your purpose.

<?php

class objSample {
    private $_myPrivateVar = false;
    public $myPublicProperty;

    public function __get() {
        return $this->_myPrivateVar;
    }

    public function __set($value) {
        $this->_myPrivateVar = $value;
    }
}

?>
Royal Bg
  • 6,944
  • 1
  • 17
  • 24
  • What `method overloading` has to do with it? `__get()` and `__set()` are meant for `undefined` class properties. That's just pointless. – Yang Aug 24 '13 at 00:45
  • this is what I thought VB.NET's `Get ... End Get Set(....)` does – Royal Bg Aug 24 '13 at 00:47