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);
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 is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I come from a java background and m trying a hand at php. Right now I m trying to pass a object to constructor of a class and trying to store a reference to it inside the class and upon a function call to this call execute a method from the stored reference.
$phpBook = new Book("Php Book", 500);
$vihaan = new Person("Vihaan", $phpBook);
Person.php
class Person
{
private $_book;
private $_name;
public function __construct($name, $book)
{
$_this->_book = $book;
$_this->_name = $name;
}
on this line
$_this->_book = $book;
I get a warning.
PHP Warning: Creating default object from empty value in /home/vihaan/workspace/AdapterPattern1/Person.php on line 12
and this function call never enter the if block as $_book seems to be empty.
public function openBook($pageNumber = 0)
{
if(!empty($_book))
{
$_book->open($pageNumber);
}
}
It's called $this, not $_this!
$this->_book = $book;
$this->_name = $name;
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.
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;
}
}
?>