Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
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
Here it is:
class objSample
{
private $_MyPrivateVar = false;
public $_MyPublicProperty;
public function getMyPrivateVar()
{
return $this->_MyPrivateVar;
}
public function setMyPrivateVar($val)
{
$this->_MyPrivateVar = $val;
}
}
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;
}
}
?>
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I created a class with a constructor (__construct()), but I don't want anyone to be able to access it. How can I do that? Thank you very much!
Edit 1:
For more detail: I created a class:
<?php
class test{
function __construct()
{
$a=1;
}
}
$t = new test;
$t->//here's the problem
?>
In my editor, when press $t->, the code hint shows the ('_construct()') and ('$a') too.
I want to ask: Can someone else can acces ('$a') or ('_construct()').
How can I prevent that,
Just make the constructor private
class Test {
private function __construct() {}
}
If you don't let any access the constructor function for your class - no one will be able to use that class as they will not be able to instantiate it.
In any case, if they have your class file they will be able to look at the source code and see the constructor.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
so as i mentioned in comments the first one shows but the second one does not show and gives me unidentified error i could just pass it to the function but is that normal ?
echo $add;//this one shows normally
if($_SERVER['REQUEST_METHOD']=="POST")
if($_POST['formname']=="Signup Form")
SignUp();
else if($_POST['formname']=="Signin Form")
SignIn();
function SignUp(){
echo $add; //this one give me unidentified erro
$bool = true;
//some code
}
function SignUp()
{
to
function SignUp()
global $add;
{
Or, better, change it to:
function SignUp($add)
{
and change your call to SignUp($add);.
The first version (with global) appears to be much tidier, but it makes things much more complex as time goes on. In hindsight I wish I hadn't learnt about global so early on – it's rarely (ever?) the right approach.
Try declaring it as a global in the function:
function SignUp(){
global $add;
echo $add;`
You can pass it as argument
function SignUp($add)
{
echo $add;
}
or Make it global
function SignUp()
{
global $add;
echo $add;
}
You can dig more into Variable Scope here
Read princip of variable scope visibilities in php, answer on your question is second example. In body of function you're looking for local scope variable, you can easy make your variable global.
http://www.php.net/manual/en/language.variables.scope.php
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
My problem is below:
class AClass{
BClass objB;
CClass objC = array();
}
$objC1 = new CClass();
$objC1->x = data; .....
$objA1 = new AClass();
$objA1->objC[] = $objC1;
So what i want to do is, there is an array of CClass objects, which should go inside AClass.
Tries arrayobjects, push etc. no luck.
Thanks in Advance.
As mentioned in my comment, PHP does not support typed class properties. I would control access to the objC property via methods which can have typed arguments. For example
class AClass {
private $objB;
private $objC = array();
public function addC(CClass $obj) {
$this->objC[] = $obj;
}
}
$objA1 = new AClass;
$objA1->addC($objC1);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I create an object/class which has a variable. The variable then needs to also have a callable function.
$a->name //return foo
$a->name->getAlias() //return foobar
The above is an example and not the desired functionality.
Thank you.
The _toString automagically can change your object if your object is being called as an string. The name needs to be an instance of a object in order to use it different ways:
class A
{
function __construct()
{
$this->name = new B();
}
}
class B
{
function __toString()
{
return 'Jamie';
}
function getAlias()
{
return 'JJAMMIIEE';
}
}
$a = new A();
print $a->name; //returns Jamie
var_dump($a->name); //returns Object B, __toString function will not be called
print $a->name->getAlias(); //returns JJAMMIIEE
Documentation here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am preparing for the ZEND Certified Engineer-Exam. Using the TestPassport-Engine "Virtual Exam", I came across this question:
Consider the following code. Which keyword should be used in line marked in bold to make this code work as intended?
abstract class Base {
protected function __construct() {}
public function create(){
// this line
return new self();
}
abstract function action();
}
class Item extends Base {
public function action () { echo __CLASS__; }
}
$item = Item::create();
$item->action();
And the correct answer is static. So, how should that look like in the end?
Simply change
public function create() {
return new self();
}
to
public static function create() {
return new static();
}
See here.