Declare array object php

How can I declare a danh sách of objects in php as a private instance variable?

In Java the declaration would look something like this private ArrayList ls và the constructor would have sầu this ls = new ArrayList();


PHPhường. allocates memory dynamically và what"s more, it doesn"t care what sort of object you store in your array.

Bạn đang xem: Declare array object php

If you want to declare your array before you use it something along these lines would work:

var $myArray = array();

Then you can store any object you like in your variable $myArray. Many people find this a strange concept to lớn grasp after working in a strict language lượt thích java.

Xem thêm: giáo dục công dân 10 bài về tình yêu

What is the difference between var $myArray = array() và $array = array() – user1114 Jan 6 "12 at 14:35

there is no difference, that's only name of that variable, you can use whatever you want :) it's lượt thích ls in your question – boobiq Jan 6 "12 at 14:43
I meant whether I use 'var' or not before the variable. – user1114 Jan 6 "12 at 14:52

user1114 it's deprecated way to declare class variables (used in PHPhường. 4), in PHP 5 you should use public/private.. – boobiq Jan 6 "12 at 19:13

you can declare it in class like

private $array = array();

và append objects (or anything) khổng lồ that array like

$array<> = some object
You can also use it associatively, by saying $array<'key'> = value; – Gaʀʀʏ Dec 13 "12 at 2:56

Thanks for contributing an answer to Stachồng Overflow!

Please be sure to answer the question. Provide details và cốt truyện your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy và cookie policy

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

“Notice: Undefined variable”, “Notice: Undefined index”, và “Notice: Undefined offset” using PHP
What did Israel Gelfand mean by “You have sầu lớn be fast only khổng lồ catch fleas,” in the context of mathematical research?
What were the parts of each of the six Seuss books that ceased publication in March 2021 that were problematic?
Does "scut work" contribution to lớn a paper as a math undergrad carry weight in grad school application?

Bulk API Upserting Datetime fields - Time is Off By Inconsistent Number of Minutes more hot questions
Staông xã Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help Mobile
Products Teams Talent Advertising Enterprise
Company About Press Work Here Legal Privacy Policy Terms of Service Contact Us
Staông chồng Exchange Network Technology Life / Arts Culture / Recreation Science Other
Stachồng Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters trò chơi Development
TeX - LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Staông chồng Overflow em Português Blender Network Engineering Cryptography Code đánh giá
Magenkhổng lồ Software Recommendations Signal Processing Emacs Raspberry Pi Stachồng Overflow на русском Code Golf
Stachồng Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)

Blog Facebook Twitter LinkedIn Instagram

site thiết kế / hình ảnh sản phẩm © 2021 Staông chồng Exchange Inc; user contributions licensed under cc by-sa. rev 2021.3.5.38718